Home > Software engineering >  Getting Last Windows Update in Powershell
Getting Last Windows Update in Powershell

Time:09-09

I am looking to write a single string to a UI displaying the last time a Windows Update was applied using powershell.

I am currently using the command $LastInstallDate = (Get-HotFix | Sort-Object -Property InstalledOn)[-1] | Select-Object -ExpandProperty 'InstalledOn'

However this returns errors on random machines I try it on

Exception getting "InstalledOn": "Exception calling "Parse" with "2" argument(s): "String was not recognized as a valid DateTime.""

When this does work occasionally, it also shows the time at 00:00:00, I would like to axe this off the output if possible so I just get the date.

TIA

CodePudding user response:

This should give you what you want:

#Gets log entries about all successfully installed patches
$successUpdatesInstalls = Get-WinEvent -ProviderName 'Microsoft-Windows-WindowsUpdateClient' -FilterXPath "*[System[EventID=19]]"

#Pick the latest one and output datetime
$successUpdatesInstalls[0].TimeCreated

CodePudding user response:

This worked for me

$LastInstallDate = Get-HotFix | Sort-Object -Property InstalledOn | Select-Object -Last 1 -Property @{l='InstalledDate';e={$_.InstalledOn.ToString("dd MMMM yyyy")
Write-Host "The last installed update or hotfix was installed on $($LastInstallDate.InstalledDate)"

Alternatively

$LastInstallDate = Get-HotFix | Sort-Object -Property InstalledOn | Select-Object -Last 1 -ExpandProperty InstalledOn
Write-Host "The last installed update or hotfix was installed on $($LastInstallDate.ToString('dd MMMM yyyy'))"

I prefer the latter as it's a DateTime variable that's assigned rather than a PSObject containing the string version of the date.

  • Related