Skip to main content
Barracuda MSP Partner Toolkit

Checking Windows Update history on a device with Powershell

The following script will save the known update history for a device to a text file.

NOTE: If the SoftwareDistribution folder has been wiped, the prior history may not be available

 

Each entry will be in the following format:

Date:  mm/dd/yyyy hh:MM:ss
Title:  [the update's title including KB #]
Description:  [the update's description]
Operation:  Installation/Uninstallation
HResult:  0x0 (or the WUA error code)
ResultCode:  NotStarted/InProgress/Succeeded/SucceededWithErrors/Failed/Aborted
ClientApplicationID:  [AutomaticUpdates normally; something else if it wasn't triggered by the WUA normally]
ServerSelection:  Default/ManagedServer/WindowsUpdate/Other
ServiceID:  
SupportUrl:  http://support.microsoft.com/help/####
UninstallationNotes:  This software update can be removed by selecting View installed updates in the Programs and Features Control Panel.
UnmappedResultCode:  0x0 or the unmapped error code. This may help explain the underlying issue to the HResult
UpdateID :  [the update GUID]
RevisionNumber :  [the update's revision #]
$output_location = 'c:\temp\updatehistory.txt';
 
function logText([string]$text, [string]$logpath) {
    $writer = [System.IO.File]::AppendText($logpath);
    $writer.WriteLine($text);
    $writer.Close();
}
 
function Dump-UpdateHistory([string]$logloc) {
    $Session = New-Object -ComObject "Microsoft.Update.Session";
    $Searcher = $Session.CreateUpdateSearcher();
    $historyCount = $Searcher.GetTotalHistoryCount();
    $ResultCodes = @{
        <# https://msdn.microsoft.com/en-us/library/aa387095(v=vs.85).aspx #>
        0  = 'NotStarted';
        1  = 'InProgress';
        2  = 'Succeeded';
        3  = 'SucceededWithErrors';
        4  = 'Failed';
        5  = 'Aborted';
    }
    $Operations = @{
        <# https://msdn.microsoft.com/en-us/library/aa387282(v=vs.85).aspx #>
        1 = 'Installation';
        2 = 'Uninstallation';
    }
    $ServerSelections = @{
        <# https://msdn.microsoft.com/en-us/library/aa387280(v=vs.85).aspx #>
        0 = 'Default';
        1 = 'ManagedServer';
        2 = 'WindowsUpdate';
        3 = 'Other';
    }
    $Searcher.QueryHistory(0, $historyCount) | foreach {
        logText (
            'Date: {0}' -f $_.Date
        ) $logloc;
        logText (
            'Title: {0}' -f $_.Title
        ) $logloc;
        logText (
            'Description: {0}' -f $_.Description
        ) $logloc;
        logText (
            'Operation: {0}' -f $Operations[$_.operation]
        ) $logloc;
        logText (
            'HResult: 0x{0:x}' -f $_.HResult
        ) $logloc;
        logText (
            'ResultCode: {0}' -f $ResultCodes[$_.ResultCode]
        ) $logloc;
        logText (
            'ClientApplicationID: {0}' -f $_.ClientApplicationID
        ) $logloc;
        logText (
            'ServerSelection: {0}' -f $ServerSelections[$_.ServerSelection]
        ) $logloc;
        logText (
            'ServiceID: {0}' -f $_.ServiceID
        ) $logloc;
        logText (
            'SupportUrl: {0}' -f $_.SupportUrl
        ) $logloc;
        logText (
            'UninstallationNotes: {0}' -f $_.UninstallationNotes
        ) $logloc;
        logText (
            'UnmappedResultCode: 0x{0:x}' -f $_.UnmappedResultCode
        ) $logloc;
        logText (
            'UpdateID : {0}' -f $_.UpdateIdentity.UpdateID
        ) $logloc;
        logText (
            'RevisionNumber: {0}' -f $_.UpdateIdentity.RevisionNumber
        ) $logloc;
        logText ('') $logloc;
    }   
}
 
Dump-UpdateHistory $output_location;
  • Was this article helpful?