Powershell script to run Windows updates.
The PSWindowsUpdate module will be used to check for and install updates.
The following script will install the module if required. If the module is already installed the version will be checked to make sure it is up to date. If needed the module will be upgraded.
# This module will install the PSWindowsUpdate Powershell module
# https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc
#Requires -RunAsAdministrator
Write-Host -ForegroundColor Gray 'Installing PSWindowsUpdate Module'
# Check if module has already been installed
Write-Host -ForegroundColor Gray 'Checking module installation status...'
try {
$module = Get-Module -ListAvailable -Name PSWindowsUpdate -ErrorAction Stop
if ( $module.Version ) {
if ([version]$module.Version -lt [version]'2.2.0.2') {
Write-Host -ForegroundColor Green "PSWindowsUpdate module version $($module.Version) is already installed - Updating module"
} else {
Write-Host -ForegroundColor Green "PSWindowsUpdate module version $($module.Version) is already installed and up to date; nothing to do"
exit
}
Write-Host -ForegroundColor Yellow 'Attempting module upgrade...'
try {
Update-Module -Name PSWindowsUpdate -ErrorAction Stop -Force -Confirm:$False
Write-Host -ForegroundColor Green 'PSWindowsUpdate module upgrade success'
}
catch {
Write-Host -ForegroundColor Red 'Error upgrading module; trying reinstall of module'
try {
Uninstall-Module -Name PSWindowsUpdate -ErrorAction Stop -Force -Confirm:$False
Install-Module -Name PSWindowsUpdate -ErrorAction Stop -Force -Confirm:$False
Write-Host -ForegroundColor Green 'PSWindowsUpdate module upgrade success'
}
catch {
Write-Host -ForegroundColor Red 'Error upgrading module'
}
}
exit
} else {
Write-Host -ForegroundColor Yellow 'PSWindowsUpdate module not installed yet, installation will proceed after requirements are checked'
}
}
catch {
Write-Host -ForegroundColor Yellow 'PSWindowsUpdate module not installed yet, installation will proceed'
}
try {
Install-Module -Name PSWindowsUpdate -ErrorAction Stop -Force -Confirm:$False
Write-Host -ForegroundColor Green 'PSWindowsUpdate module is now installed'
}
catch {
$message = $_.Exception.message
Write-Error 'Error installing module:'
throw $message
}
To check for updates and install them silently:
Install-WindowsUpdate -MicrosoftUpdate -IgnoreUserInput -WhatIf -Verbose -AcceptAll
The above command will not reboot after update installation. To automatically reboot, add the -AutoReboot
flag:
Install-WindowsUpdate -MicrosoftUpdate -IgnoreUserInput -WhatIf -Verbose -AcceptAll -AutoReboot
The above pair of scripts can be added to PDQ Deploy. A package can be created with step 1 being the powershell script to install module and step 2 being the command to install updates.