Functions/Start-WindowsUpdate.ps1

# Windows Update by using the module PSWindowsUpdate
function Start-WindowsUpdate {
    if ($isAdmin -ne $true) {
        Write-Output "Please run this script with admin privileges."
        Break
    }
    else {
        $moduleName = "PSWindowsUpdate"
        if (Get-Module -ListAvailable -Name $moduleName) {
            Import-Module $moduleName
            Write-Output "Module $moduleName exists."
            $currentDate = Get-Date -Format "yyyy-MM-dd"
            $desktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
            $outputFilePath = "$desktopPath\$currentDate-WindowsUpdate_Patches_Installed.txt"
            Write-Output "Installing Windows Updates... Please be patient!"
            Install-WindowsUpdate -MicrosoftUpdate -UpdateType Software -AcceptAll | Out-File $outputFilePath -force
            Write-Output "All patches have been installed. Thanks for your patience."
        } 
        else {
            Write-Output "Module $moduleName does not exist."
            try {
                Install-Module -Name $moduleName -Force
                Write-Output "Module $moduleName has been installed."
            }
            catch {
                Write-Error $_.ErrorDetails
            }
        }
    }
}