Finding Installed Updates (and searching for missing) (Part 4)

Sometimes, the Microsoft.Update.Session object is used to check whether a given update is present on a machine. Some authors query the text title string of updates like this:

#requires -Version 3.0 

function Get-UpdateInstalled([Parameter(Mandatory)]$KBNumber)
{
    $Session = New-Object -ComObject "Microsoft.Update.Session"
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()

    $status = @{
        Name="Operation"
        Expression= {
            switch($_.operation)
            {
                1 {"Installation"}
                2 {"Uninstallation"}
                3 {"Other"}
            }
        }
    }

    $Searcher.QueryHistory(0, $historyCount) | 
    Where-Object {$_.Title -like "*KB$KBNumber*" } |
    Select-Object -Property Title, $status, Date
}

function Test-UpdateInstalled([Parameter(Mandatory)]$KBNumber)
{
    $update = Get-UpdateInstalled -KBNumber $KBNumber |
    Where-Object Status -eq Installation |
    Select-Object -First 1
      
    return $update -ne $null
}

Test-UpdateInstalled -KBNumber 2267602
Get-UpdateInstalled -KBNumber 2267602 | Out-GridView

Note that this approach is not only much faster, but by splitting up the task into two functions, you can also dump the titles of all installed updates:

 
PS> Get-UpdateInstalled -KBNumber 2267602

Title                                                                        Operation    Date       
-----                                                                        ---------    ----       
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.348.0)  Installation 28.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.281.0)  Installation 27.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.237.0)  Installation 26.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.191.0)  Installation 25.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.139.0)  Installation 24.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.95.0)   Installation 22.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.93.0)   Installation 22.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.28.0)   Installation 21.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.249.13.0)   Installation 20.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.247.1068.0) Installation 19.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.247.1010.0) Installation 18.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.247.969.0)  Installation 17.07.20...
Definitionsupdate für Windows Defender – KB2267602 (Definition 1.247.966.0)  Installation 17.07.20... 
 

Twitter This Tip! ReTweet this Tip!

  • Former Member
    Former Member

    I had to make the following change to get the Test-UpdateInstalled function to work.

    From:

       $update = Get-UpdateInstalled -KBNumber $KBNumber |

       Where-Object Status -eq Installation |

       Select-Object -First 1

    To:

       $update = Get-UpdateInstalled -KBNumber $KBNumber |

       Where-Object Operation -eq Installation |

       Select-Object -First 1