locked
Get-itemproperty filtered by date returning everything in the directory when it should return nothing RRS feed

  • Question

  • I'm trying to verify installation of some items in the registry. I would like to return anything installed in the last 14 days. I'm using this:

    Get-ItemProperty HKLM:\Software\Microsoft\Updates\*\* |Select-Object -property PSComputerName, PSChildName, InstalledDate |where {$_.InstalledDate -gt (get-date).adddays(-14).ToString('M/d/yyyy')}

    When I have a value present for that timeframe, it works. However, when there is nothing that meets the criteria, it returns the information for everything with an InstalledDate in the path  I'm new at this so I'm wondering what am I doing wrong?

    Monday, October 16, 2017 8:14 PM

Answers

  • You cannot compare dates as strings.

    This is easier:

    get-hotfix |
         where{$_.InstalledOn  -gt (get-date).adddays(-14)}|
         select hotfixid,installedon

    or:

    Get-ItemProperty HKLM:\Software\Microsoft\Updates\*\* | 
    	Where-Object{
    		Try{[datetime]$_.InstalledDate -gt (get-date).adddays(-14)}
    		Catch{}
    	} | Select-Object PackageName, InstalledDate


    \_(ツ)_/

    • Marked as answer by Tigger4066 Monday, October 16, 2017 9:30 PM
    Monday, October 16, 2017 8:44 PM

All replies

  • You cannot compare dates as strings.

    This is easier:

    get-hotfix |
         where{$_.InstalledOn  -gt (get-date).adddays(-14)}|
         select hotfixid,installedon

    or:

    Get-ItemProperty HKLM:\Software\Microsoft\Updates\*\* | 
    	Where-Object{
    		Try{[datetime]$_.InstalledDate -gt (get-date).adddays(-14)}
    		Catch{}
    	} | Select-Object PackageName, InstalledDate


    \_(ツ)_/

    • Marked as answer by Tigger4066 Monday, October 16, 2017 9:30 PM
    Monday, October 16, 2017 8:44 PM
  • That worked!  Thanks!

    Monday, October 16, 2017 9:30 PM