Skip to content

Instantly share code, notes, and snippets.

@rgl
Created April 16, 2017 14:29
Show Gist options
  • Select an option

  • Save rgl/d124d37ac6e8c5ebe4b3aab0fdf99bfb to your computer and use it in GitHub Desktop.

Select an option

Save rgl/d124d37ac6e8c5ebe4b3aab0fdf99bfb to your computer and use it in GitHub Desktop.
List Windows update history
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
trap {
Write-Output "ERROR: $_"
Write-Output (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1')
Exit 1
}
Write-Output 'Windows update history:'
$updateSearcher = (New-Object -ComObject 'Microsoft.Update.Session').CreateUpdateSearcher()
$updateSearcher.QueryHistory(0, $updateSearcher.GetTotalHistoryCount()) `
| Where-Object {$_.ResultCode -ne $null} `
| ForEach-Object {
$result = switch ($_.ResultCode) {
0 { 'NotStarted' }
1 { 'InProgress' }
2 { 'Succeeded' }
3 { 'SucceededWithErrors' }
4 { 'Failed' }
5 { 'Aborted' }
default { $_ }
}
New-Object -TypeName PSObject -Property @{
Date = $_.Date
By = $_.ClientApplicationID
Result = $result
Title = $_.Title
}
} `
| Sort-Object -Descending Date `
| Format-Table -Property Date,Result,By,Title -AutoSize `
| Out-String -Stream -Width ([int]::MaxValue) `
| ForEach-Object {$_.TrimEnd()}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment