Compare date using powershell query RRS feed

  • Question

  • Hi,

    i would like to select a group of data by comparing several critiria as below.

    1. CustomAttribute5 less equal today date

    2. CustomAttribute8 not NULL

    3. CustomAttribute5 not NULL

    Is below script correct?

    $todayDate = Get-Date -format dd-MM-yyyy

    $allUser = Get-Mailbox -resultsize unlimited | where {$_.CustomAttribute5.toString("dd-MM-yyyy") -le $todayDate -and !$_CustomAttribute8 -and !$_.CustomAttribute5}

    • Moved by Bill_Stewart Wednesday, September 13, 2017 10:00 PM User can test code all by himself
    Wednesday, August 9, 2017 7:11 AM

All replies

  • You cannot compare dates as strings.

    where {$_.CustomAttribute5 -le [datetime]::Today ....

    It is more like that the attribute is a string:

    where {[datetime]($_.CustomAttribute5) -le [datetime]::Today ....

    Here is the filter for nulls:

    where {($_CustomAttribute8 -and $_.CustomAttribute5) -and [datetime]($_.CustomAttribute5) -le [datetime]::Today }


    • Edited by jrv Wednesday, August 9, 2017 3:26 PM
    Wednesday, August 9, 2017 3:24 PM