Infrastructure

Programmatically List the Installed Updates

I had a need to get a quick listing of the OS version and the updates from a series of computers. So I wrote the following power shell script that outputs to a specified file, the computer name, OS info, and then a list of the updates that have been installed. Save it to a PS1 file or run it line by line.

Here is the Script:

$Machine = gc env:computername

$OutPath = "c:\temp\Updates-$Machine.txt"

$QueryString = Gwmi Win32_OperatingSystem -Comp $Machine

$QueryString = $QueryString.Caption

"$machine : $querystring" >$outpath

$wu = new-object -com "Microsoft.Update.Searcher"

$totalupdates = $wu.GetTotalHistoryCount()

$wu.QueryHistory(0,$totalupdates) | FL date,title,description >>$outpath

So what does it do?

Line 1 gets the local machine name and stores it in a variable. This could be replaced by a remote system name (with the proper ports and permissions).

    GC env:computername returns the localhost name in powershell

Line 2 sets the output path.

Line 3 queries WMI for the OS of the $machine

Line 4 prints the Machine name and the OS to the output file.

Line 5 and 6 get the number of updates that have been installed and stores it in a variable, ($totalupdates) (This is used to iterate through each of the updates and return information).

Line 7 Queries the Microsoft.Update.Searcher for each of the installed updates, returns the date, title, and description, and outputs it to the $outpath.

Note: there was more information available, but I only wanted those three pieces of information.

Note: I am appending ">>" to the $outpath throughout the script.

Leave a Reply