Windows Updates Powershell command

Duchemin, Dominique 2,006 Reputation points
2022-01-10T18:31:27.2+00:00

Hello,

I was using this command:

$lastpatch = (get-hotfix -computername $hostname | select InstalledOn | Sort-Object installedon -Descending | Select-Object -first 1).installedon;

or
PS C:\Windows\system32> get-hotfix -computername SOPCPCWS1 | select InstalledOn | Sort-Object installedon -Descending |
Select-Object -first 1

InstalledOn


1/24/2021 12:00:00 AM

and I tried also

> Get-wmiobject -class win32_quickfixengineering | Sort InstalledOn

Samething the KN890830 is not listed and as is the last windows updates is incorrect!!!

But I noticed that the "Windows Malicious Software Removal Too x64 - v5.96 (KB890830) is not detected as it is not a hotfix ...

how to get the last "windows updates" run through powershell? (including the KB890830)

Thanks,
Dom
163667-2022-01-10-9-49-34-sopcpcws1-windows-updates.png163659-2022-01-10-10-04-32-sopcpcws1-updates.png

Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-01-10T19:18:39.417+00:00

    You could try something like this:

    # Convert Wua History ResultCode to a Name   
    # 0, and 5 are not used for history   
    # See https://msdn.microsoft.com/en-us/library/windows/desktop/aa387095(v=vs.85).aspx  
      
    function Convert-WuaResultCodeToName {  
        param( [Parameter(Mandatory = $true)]  
            [int] $ResultCode  
        )  
        # https://learn.microsoft.com/en-us/windows/win32/api/wuapi/ne-wuapi-operationresultcode  
        $Result = $ResultCode  
        switch ($ResultCode) {  
            0 { $Result = "NotStarted; break"}  
            1 { $Result = "InProgress"; break }  
            2 { $Result = "Succeeded"; break }  
            3 { $Result = "Succeeded With Errors"; break }  
            4 { $Result = "Failed"; break }  
            5 { $Result = "Aborted"; break }  
        }  
        $Result  
    }  
    function Get-WuaHistory {  
        # Get a WUA Session  
        $session = New-Object -ComObject 'Microsoft.Update.Session'  
        $searcher = $session.CreateUpdateSearcher()  
        $historyCount = $searcher.GetTotalHistoryCount()  
        # Query the latest 1000 History starting with the first record  
        $session.QueryHistory("", 0, $historyCount) |  # criteria, start index, count  
            ForEach-Object {  
                [PSCustomObject]@{  
                    Result = (Convert-WuaResultCodeToName -ResultCode $_.ResultCode)  
                    Date = $_.Date  
                    Title = $_.Title  
                    SupportUrl = $_.SupportUrl  
                    Product = ($_.Categories | Where-Object { $_.Type -eq 'Product' } | Select-Object -First 1 -ExpandProperty Name)  
                    UpdateId = ($_.UpdateIdentity.UpdateId)  
                    RevisionNumber = ($_.UpdateIdentity.RevisionNumber)  
                    Description = $_.Description  
                }  
            } | Where-Object { ![String]::IsNullOrWhiteSpace($_.title) }     #remove null(?) records, return selected properties  
    }  
      
    Get-WuaHistory | export-csv c:\junk\updates.csv -notypeinfo #Format-Table -AutoSize  
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Duchemin, Dominique 2,006 Reputation points
    2022-01-10T19:51:44.25+00:00

    Hello,

    Should it work in Windows Server 2008?
    I tested in Windows Server 2016 and 2019 it is working fine as well as Windows 10.
    But Windows Server 2008 and Windows Server 2008 R2 does not give any result as I was not able to run at least the command:
    Install-Module -name PSWindowsUpdate
    PS C:\Windows\system32> import-module -name PSWindowsUpdate
    Import-Module : The specified module 'PSWindowsUpdate' was not loaded because no valid module file was found in any mod
    ule directory.
    At line:1 char:14

    • import-module <<<< -name PSWindowsUpdate
    • CategoryInfo : ResourceUnavailable: (PSWindowsUpdate:String) [Import-Module], FileNotFoundException
    • FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

    Checking for the download...

    Thanks,
    Dom

    0 comments No comments

  2. Duchemin, Dominique 2,006 Reputation points
    2022-01-22T19:28:51.907+00:00

    Hello,

    I find out that this issue was due to the ESU licensing not renewed and as is the patches were not picked/listed. After renewing the ESU license this is working and patches came available and applied...

    Thanks,
    Dom

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.