Show Windows Updates Locally With PowerShell

I wanted to be able to quickly show the Windows Updates on a server. This came about during a discussion about auditing.

Of course, there is no point in re-inventing the wheel so I had a quick Google and  found a couple of posts on from  Hey Scripting Guy blog and one from Tim Minter. Neither quite did what I wanted so I modified them as follows.

We start by creating a Update object and find the total number of updates and setting them to a variable $History which we pass to the QueryHistory Method. This enables us to show all the updates

image

Passing this to Get-Member shows

image

which doesn’t show the KB so I read a bit more and found Tom Arbuthnot’s Blog Post

image

this transforms the ResultCode Property to something meaningful and places the KB in its own column.

I have created a function called Show-WindowsUpdatesLocal It’s Local because doing it for a remote server takes a different approach but I will show that another day.

This means you can call the function and use the results however you like

Show-WindowsUpdatesLocal

image

Show-WindowsUpdatesLocal| Select Date, HotfixID, Result|Format-Table -AutoSize

image

Show-WindowsUpdatesLocal|Where-Object {$_.Result -eq ‘Failed’} |Select Date, HotfixID, Result,Title|Format-Table -AutoSize

image

Output to file

Show-WindowsUpdatesLocal|Format-Table -AutoSize|Out-File c:\temp\updates.txt

image

Output to CSV

Show-WindowsUpdatesLocal|Export-Csv c:\temp\updates.csv

image

You can get the code here

#############################################################    #########
#
# NAME: Show-WindowsUpdatesLocal.ps1
# AUTHOR: Rob Sewell https://blog.robsewell.com
# DATE:22/09/2013
#
# COMMENTS: Load function to show all windows updates locally
#
# USAGE:  Show-WindowsUpdatesLocal
#         Show-WindowsUpdatesLocal| Select Date, HotfixID,     Result|Format-Table -AutoSize
#         Show-WindowsUpdatesLocal|Where-Object {$_.Result     -eq 'Failed'} |Select Date, HotfixID, Result,Title|    Format-Table -AutoSize
#         Show-WindowsUpdatesLocal|Format-Table -AutoSize|    Out-File c:\temp\updates.txt
#         Show-WindowsUpdatesLocal|Export-Csv     c:\temp\updates.csv
#        

Function Show-WindowsUpdatesLocal {
    $Searcher = New-Object -ComObject Microsoft.Update.    Searcher
    $History = $Searcher.GetTotalHistoryCount()
    $Updates = $Searcher.QueryHistory(1, $History)
    # Define a new array to gather output
    $OutputCollection = @() 
    Foreach ($update in $Updates) {
        $Result = $null
        Switch ($update.ResultCode) {
            0 { $Result = 'NotStarted'}
            1 { $Result = 'InProgress' }
            2 { $Result = 'Succeeded' }
            3 { $Result = 'SucceededWithErrors' }
            4 { $Result = 'Failed' }
            5 { $Result = 'Aborted' }
            default { $Result = $_ }
        }
        $string = $update.title
        $Regex = “KB\d*”
        $KB = $string | Select-String -Pattern $regex |     Select-Object { $_.Matches }
        $output = New-Object -TypeName PSobject
        $output | add-member NoteProperty “Date” -value     $Update.Date
        $output | add-member NoteProperty “HotFixID” -value     $KB.‘ $_.Matches ‘.Value
        $output | Add-Member NoteProperty "Result" -Value     $Result
        $output | add-member NoteProperty “Title” -value     $string
        $output | add-member NoteProperty “Description”     -value $update.Description
        $OutputCollection += $output
    }
    $OutputCollection
}
Built with Hugo
Theme Stack designed by Jimmy