Harvey
Harvey

Reputation: 1418

Powershell, How to get date of last Windows update install or at least checked for an update?

I am trying to find a way of retrieving the date/time of which the last windows update was either installed, or checked for.

So far I have found a function that allows to list recent Windows Updates, but it is far too much data and too bloated for such a simple function. Secondly I have tried to access the registry although I am having no luck in retriving the value I am after.

I am testing this on a Windows 10 Machine although the software will probably reside on Windows Server 2012 R2.

Here is an example of some of the code I have tried:

$key = “SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install” 
$keytype = [Microsoft.Win32.RegistryHive]::LocalMachine 
$RemoteBase = [Microsoft.Win32.RegistryKey]::OpenBaseKey($keytype,"My Machine") 
$regKey = $RemoteBase.OpenSubKey($key) 
$KeyValue = $regkey.GetValue(”LastSuccessTime”) 

$System = (Get-Date -Format "yyyy-MM-dd hh:mm:ss")  

Also, just trying the Get-ChildItem

$hello = Get-ChildItem -Path “hkcu:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\”

foreach ($a in $hello) {

$a

}

I've checked in regedit and this key does not exist. Going to the "Windows Update" path shows only App Updates and not Windows updates.

EDIT I seem to be closer to my goal with this line: Get-HotFix | Where {$_.InstallDate -gt 30}

However how to I only retrive those of which have been installed in the last 30 days? And this doesnt show many results, even using Select $_.InstallDate

Upvotes: 11

Views: 91734

Answers (5)

Mr. Mike
Mr. Mike

Reputation: 249

Pre-PowerShell 7

Users can use the code by Loïc MICHEL for querying via WMI:

Get-WmiObject Win32_QuickFixEngineering | Sort-Object -Property InstalledOn -Descending

Post-PowerShell 7

PowerShell 7 deprecated WMI as a result of switching to .NET Core. Instead, you can utilize CIM

Get-CimInstance Win32_QuickFixEngineering | Sort-Object -Property InstalledOn -Descending

Alternative

kayess and Martin posted excellent, simple example using Get-HotFix, which works for PowerShell 5.1 through 7.3 (and possibly other versions as well).

Get-HotFix | Sort-Object -Property InstalledOn -Descending

Upvotes: 1

Martin
Martin

Reputation: 71

Using PowerShell, you can get the date of the las Windows update like this:

$lastWindowsUpdate = (Get-Hotfix | Sort-Object -Property InstalledOn -Descending | Select-Object -First 1).InstalledOn

Upvotes: 3

John Burkhead
John Burkhead

Reputation: 81

Here you have how to know the date and time of the last Windows update in a single line of Powershell:

(New-Object -com "Microsoft.Update.AutoUpdate"). Results | fl

You also have the following script to check it massively in Windows Server:

$ servers = Get-ADComputer -Filter {(OperatingSystem-like "* windows * server *") -and (Enabled -eq "True")} -Properties OperatingSystem | Sort Name | select -Unique Name

foreach ($ server in $ servers) {
write-host $ server.Name

   Invoke-Command -ComputerName $ server.Name -ScriptBlock {
(New-Object -com "Microsoft.Update.AutoUpdate"). Results}
}

Extracted from: https://www.sysadmit.com/2019/03/windows-update-ver-fecha-powershell.html

Upvotes: 8

OuttaBeer
OuttaBeer

Reputation: 59

Get-HotFix |?{$_.InstalledOn -gt ((Get-Date).AddDays(-30))}

Upvotes: 5

Loïc MICHEL
Loïc MICHEL

Reputation: 26180

an option :

 gwmi win32_quickfixengineering |sort installedon -desc 

Another alternative, using the com object Microsoft.Update.Session can be find here : https://p0w3rsh3ll.wordpress.com/2012/10/25/getting-windows-updates-installation-history/ in short :

$Session = New-Object -ComObject Microsoft.Update.Session 
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa386532%28v=vs.85%29.aspx
$Searcher.QueryHistory(0,$HistoryCount) | ForEach-Object {$_}

Upvotes: 17

Related Questions