Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EmmanuelTsouris/34a14d10631cdc68aa0b60c27191ca2e to your computer and use it in GitHub Desktop.
Save EmmanuelTsouris/34a14d10631cdc68aa0b60c27191ca2e to your computer and use it in GitHub Desktop.
PowerShell for Windows Updates
#Scan for available updates
function Get-AvailableUpdates {
  [CmdletBinding()]
  param()
  $ci = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession 
  $result = $ci | Invoke-CimMethod -MethodName ScanForUpdates -Arguments @{SearchCriteria="IsInstalled=0";OnlineScan=$true}
  $result.Updates
}
#Install all available updates
function Install-AvailableUpdates {
[CmdletBinding()]
param()
$ci = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
Invoke-CimMethod -InputObject $ci -MethodName ApplyApplicableUpdates
}
#list installed updates
function Get-InstalledUpdates {
[CmdletBinding()]
param()
$ci = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
$result = $ci | Invoke-CimMethod -MethodName ScanForUpdates -Arguments @{SearchCriteria="IsInstalled=1";OnlineScan=$true}
$result.Updates
}
# From <https://richardspowershellblog.wordpress.com/2016/12/22/applying-updates-through-wsus/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment