Home > OS >  How would I disaply only the last two days of windows updates in powershell?
How would I disaply only the last two days of windows updates in powershell?

Time:06-17

Okay, so I'm in the process of writing out a script, and I'm at a point where I want to count the number of updates installed in the last day or so. I'm using Get-CIMInstance because the get-wuhistory command outputs dates in a weird format. So, here's the crux of my problem.

Here's the unfiltered output:

    Get-CimInstance -class win32_quickfixengineering                                                             

Source        Description      HotFixID      InstalledBy          InstalledOn
------        -----------      --------      -----------          -----------
              Update           KB5013887     NT AUTHORITY\SYSTEM  6/9/2022 12:00:00 AM
              Security Update  KB4560366     NT AUTHORITY\SYSTEM  6/23/2020 12:00:00 AM

So, when I run the following, I get nothing, no error, no output, it just kicks to another prompt.

Get-CimInstance -class win32_quickfixengineering | Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-2))}

What am I missing here? I've got to be overlooking something simple, but I've wracked my brain and can't seem to figure it out.

CodePudding user response:

This can be done using Get-Hotifx.

Get-HotFix |Where-Object {$_.Installedon -gt (Get-Date).AddDays(-2)}

or if you want to use Get-GmiInstance, try this.

 Get-CimInstance -class win32_quickfixengineering  |Where-Object {$_.InstalledOn -gt (Get-Date).AddDays(-2)}

Note - As others mentioned in the comments, you need to make sure that at least one update exists which got installed in the last two days. Else, you will not get any result. I did tested the below commandlets on my machine and works well.

CodePudding user response:

Your code is not returning any result because you haven't install any updates in the last 2 days.

Try this to test:

Get-CimInstance -class win32_quickfixengineering | Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-200))}

You could even turn this into a reusable filter.

function Older-Than {
  param
  (
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = 'Data to filter')]
    $InputObject,
    [Parameter(Mandatory = $true, HelpMessage = 'Less than how many days ago?')]
    [Int]$days
  )
  process {
    if ($InputObject.Installedon -gt ((Get-Date).Adddays( - ($days)))) {
      $InputObject
    }
  }
}

So that Get-CimInstance -ClassName win32_quickfixengineering | Older-Than -Days 20 would output:

PS C:\Powershell\Scripts> . "c:\Powershell\Scripts\filter.ps1"

Source        Description      HotFixID      InstalledBy          InstalledOn
------        -----------      --------      -----------          -----------
              Update           KB5013887     NT AUTHORITY\SYSTEM  6/16/2022 12:00:00 AM
              Security Update  KB5014699     NT AUTHORITY\SYSTEM  6/16/2022 12:00:00 AM
              Update           KB5014035     NT AUTHORITY\SYSTEM  6/15/2022 12:00:00 AM
  • Related