Checking Windows Updates

by May 26, 2014

To check all installed updates on a Windows box, there is a COM library you can use.
Unfortunately, this library isn't very intuitive to use, nor does it work remotely.

So here is a PowerShell function called Get-WindowsUpdate. It gets the locally installed updates by default, but you can also specify one or more remote machines and then retrieve their updates.

Remote access is done through PowerShell remoting, so it will work only if the remote machine has PowerShell remoting enabled (Windows Server 2012 enables PS remoting by default, for example), and you need to have local Administrator privileges on the remote machine.

function Get-WindowsUpdate
{
  [CmdletBinding()]
  param
  (
    [String[]]
    $ComputerName,
    $Title = '*',
    $Description = '*',
    $Operation = '*'
  )
  
  $code = {
    param
    (
      $Title,
      $Description
    )


    $Type = @{
      name='Operation'
      expression={
    
    switch($_.operation)
    {
            1 {'Installed'}
            2 {'Uninstalled'}
            3 {'Other'}
    }
 }
}
    
    
    $Session = New-Object -ComObject 'Microsoft.Update.Session'
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $Searcher.QueryHistory(0, $historyCount) | 
    Select-Object Title, Description, Date, $Type |
    Where-Object { $_.Title -like $Title } |
    Where-Object { $_.Description -like $Description } |
    Where-Object { $_.Operation -like $Operation }
  }

  $null = $PSBoundParameters.Remove('Title')
  $null = $PSBoundParameters.Remove('Description')
  $null = $PSBoundParameters.Remove('Operation')

  Invoke-Command -ScriptBlock $code @PSBoundParameters -ArgumentList $Title, $Description
}

The function also supports filtering, so to get all installed Office updates, this is all you need to do:

Twitter This Tip! ReTweet this Tip!