Skip to main content

PowerShell function to show Windows Update History from the System Event Log.

function Get-WindowsUpdatesHistory
{
    <#
    .SYNOPSIS
        Get Windows update history.

    .DESCRIPTION
        Retrieve Windows Update history from the System Event Log.

    .NOTES
        Windows Update Event IDs:

        19 - Installation Successful: Windows successfully installed the following update.
        20 - Installation Failure: Windows failed to install the following update with error.
        43 - Installation Started: Windows has started installing the following update.

    .LINK
        https://github.com/actions/virtual-environments/blob/5181bf0714e118b8eff1286bf3fb006ec692223e/images/win/scripts/ImageHelpers/InstallHelpers.ps1
    #>

    [CmdletBinding()]
    param()

    $allEvents = @{}

    $filter = @{
        LogName = 'System'
        Id = 19, 20, 43
        ProviderName = 'Microsoft-Windows-WindowsUpdateClient'
    }

    $events = Get-WinEvent -FilterHashtable $filter -ErrorAction SilentlyContinue | Sort-Object Id

    foreach ($event in $events)
    {
        switch ($event.Id)
        {
            19
            {
                $status = 'Successful'
                $title = $event.Properties[0].Value
                $allEvents[$title] = ''
                break
            }
            20
            {
                $status = 'Failure'
                $title = $event.Properties[1].Value
                $allEvents[$title] = ''
                $event.Properties
                break
            }
            43
            {
                $status = 'InProgress'
                $title = $event.Properties[0].Value
                break
            }
        }

        if ($status -eq 'InProgress' -and $allEvents.ContainsKey($title))
        {
            continue
        }

        [PSCustomObject]@{
            Status = $status
            Title = $title
        }
    }
}

Get-WindowsUpdatesHistory