Determining if multiple security updates installed

Creating a package for installing Internet Explorer 11, which has a number of security updates to install first as prerequisites (https://support.microsoft.com/en-us/kb/2847882).

To check if one hotfix is installed, I know I can use the following line of code, then check if the variable is not null or not empty:
$hotfix_installed = (Get-Hotfix | where {$_.HotFixId -like “KB2929437”})

I’m wondering if there’s a more efficient way of doing this than simply calling that line of code X number of times and just changing the KB number?
Example:
$hotfix_installed = (Get-Hotfix | where {$.HotFixId -like “KB2929437”})
$hotfix_installed = (Get-Hotfix | where {$
.HotFixId -like “KB123456”})
$hotfix_installed = (Get-Hotfix | where {$_.HotFixId -like “KB345678”})

Because if my thinking is correct, if I call that same line of code X number of times, that seems a waste of processing time/power as opposed to calling the code once. Essentially, I think it’s the difference between saying “Check all updates installed and see if this one update is part of the list, then I’m going to do that all over again, then again, then again, etc.” vs. “Check all updates installed one time only and see if these X number of updates are part of the list”.

Thanks!

<code>[array]$hotfixes = &quot;KB2929437&quot;,&quot;KB2929438&quot;,&quot;KB2929439&quot;
foreach($hotfix in $hotfixes){
   if(Get-Hotfix | where-object {$_.HotFixID -like $hotfix}){
      write-host &quot;hit&quot;
   }
}</code>

Should work :slight_smile:

You could also use this example of a for each loop:

Hotfixes you want to search for

$hotfixlist = ‘kb2693643’,‘kb3118754’,‘bogushotfix’

Gather all hotfixes installed

$Hotfixes = Get-HotFix

Search through all installed hotfixes for the ones you have

foreach ($hotfix in $hotfixlist){
$i = $hotfixes | Where-Object -FilterScript {$_.HotFixID -eq $hotfix}
if($i){$hotfix_Installed = $True}Else{ }
}

Original Way: 1404.0431 Milliseconds
Second Way: 1388.1292 Milliseconds
Third Way: 209.6517 Milliseconds

This way, you only perform the Get-Hotfix lookup once, and the real work is just done with a simple where-object filter. Example measuring all three methods:

$OriginalWay = Measure-Command {
$hotfix_installed = (Get-Hotfix | where {$.HotFixId -like “KB2693643”})
$hotfix_installed = (Get-Hotfix | where {$
.HotFixId -like “KB3116900”})
$hotfix_installed = (Get-Hotfix | where {$.HotFixId -like “KB3116908”})
$hotfix_installed = (Get-Hotfix | where {$
.HotFixId -like “KB3118754”})
$hotfix_installed = (Get-Hotfix | where {$.HotFixId -like “KB3120677”})
$hotfix_installed = (Get-Hotfix | where {$
.HotFixId -like “KB3124200”})
$hotfix_installed = (Get-Hotfix | where {$_.HotFixId -like “KB3133431”})
}

$secondway = Measure-Command {
$Hotfixes =
‘KB2693643’,'KB3116900',
‘KB3116908’,'KB3118754',
‘KB3120677’,'KB3124200',
‘KB3133431’
foreach($hotfix in $hotfixes){
if(Get-Hotfix | where-object {$_.HotFixID -like $hotfix}){
$hotfix_installed = $true
}
}
}

$thirdway = Measure-Command {
$HotfixList =
‘KB2693643’,'KB3116900',
‘KB3116908’,'KB3118754',
‘KB3120677’,'KB3124200',
‘KB3133431’

# Gather all hotfixes installed
$Hotfixes = Get-HotFix

# Search through all installed hotfixes for the ones you have
foreach ($hotfix in $hotfixlist){
$i = $hotfixes | Where-Object -FilterScript {$_.HotFixID -eq $hotfix}
if($i){$hotfix_Installed = $True}Else{ }
}

} # MC

“Original Way:” + $OriginalWay.TotalMilliseconds
“Second Way:” + $secondway.TotalMilliseconds
“Third Way:” + $thirdway.TotalMilliseconds