Problem with where-Object filtering

Welcome Forums General PowerShell Q&A Problem with where-Object filtering

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5457

    Points: 0
    Rank: Member

    by grantc at 2013-01-21 16:05:55

    Hi Guys,

    I was hoping someone could help me with a where-object issue I'm having with filtering an array..

    Here's where I'm at..

    PS> $grabdate

    Monday, 22 October 2012 10:57:57 AM

    PS> $wmicarraycheck | where {$_.installedon -ge $grabdate}

    CSName description hotfixid InstalledOn
    —— ———– ——– ———–
    Security Update KB2778930 18/01/13
    Security Update KB2770660 18/01/13
    Security Update KB2761465 18/01/13
    Security Update KB2799329 18/01/13
    Security Update KB2785220 18/01/13
    Security Update KB2757638 18/01/13
    Security Update KB2753842 18/01/13
    Security Update KB2758857 18/01/13
    Security Update KB2742601 18/01/13
    Security Update KB2729453 18/01/13
    Security Update KB978338 19/10/12
    Security Update KB2731847 19/10/12
    Security Update KB2744842 19/10/12
    Security Update KB2712808 19/10/12

    I cannot get it to filter on the InstalledOn date. It always displays the entire array. I did create a custom Label for InstalledOn as shown below;

    PS> $wmicarraycheck | gm installedon

    TypeName: Selected.System.Management.Automation.PSCustomObject

    Name MemberType Definition
    —- ———- ———-
    InstalledOn NoteProperty System.String InstalledOn=01/01/01

    Any assistance would be much appreciated.


    by DonJ at 2013-01-21 16:16:59

    Yeah, your InstalledOn is a string, and you're comparing it to a raw date. That's probably the issue. Sometimes it'll implicitly figure it out, sometimes not. You'll want to convert InstalledOn to a real date. I don't know how you're defining InstalledOn, but you want it to be a DateTime, not a String. You can use the [datetime] (I think, maybe it's just [date]) accelerator to convert a string, if that helps.

    by grantc at 2013-01-21 16:55:22

    Thanks for that, that helps steer me on the right path.. Just having trouble with the [convert] syntax..

    PS> $wmicarraycheck | select @{Name='InstalledOn';Expression={System.DateTime]::Parse($_.installedon)} }


    How would I use the [String]]] Option in this case? ..

    by DonJ at 2013-01-21 16:59:37

    Syntax. Mismatched square brackets around [datetime] and you don't need system.

    [datetime]"1/1/2000" will call the constructor. You're over thinking the .NET bits.

    by grantc at 2013-01-21 18:37:01

    Magic – I've managed to get it working. Thanks for your help.

The topic ‘Problem with where-Object filtering’ is closed to new replies.