How do I compare a whenCreated property?

Hi,

My goal is to list any OU created today, so I have the following:

$Today = Get-Date -F d

Get-AdorganizationalUnit -Filter {whenCreated -eq $Today}

The result of $Today is a String in the format mm/dd/yyyy, so the format is correct.

The whenCreated OU property is a DateTime format in the format mm/dd/yyyy hh:mm:ss AM/PM.

I'd like to keep the Filter on the Get-ADOrganizationalUnit cmdlet and not use a Where object.

Any suggestions on how to perform

August 20th, 2015 12:56pm

Did you try removing the formatting of $Today so it remains a DateTime object? It shouldn't matter what "format" whenCreated is in, as long as its type is of DateTime, then you should be able to compare.

When you list out whenCreated, it is calling the ToString() method which does the forma

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 1:03pm

Thanks for the response clayman2

When I leave the formatting off of $Today, a piping of $Today to GM indicates a System.DateTime object, however the Filter doesn't seem to work and Im guessing its because Im indicating '-eq' in the Filter and the DateTime objects have time in them (ie. hh:mm:ss), so they will never be equal.

August 20th, 2015 1:15pm

You are correct, due to the time portion you will never have a match

You can try the following, but I believe there may be issues when trying to compare DateTime objects as strings

$Today = Get-Date

Get-AdorganizationalUnit -Filter {whenCreated.ToShortDateString() -eq $Today.ToShortDateString()}
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 1:51pm

This should work:

$Today = [datetime]::Today
Get-ADOrganizationalUnit -Filter {whenCreated -gt $Today} -Properties whenCreated

  • Marked as answer by SdeDot 12 hours 58 minutes ago
August 20th, 2015 1:56pm

This should work:

$Today = [datetime]::Today
Get-ADOrganizationalUnit -Filter {whenCreated -gt $Today} -Properties whenCreated


But that should still fail since the time is set to 12:00:00 AM, and if a OU was created at 12:30:45 AM, it will not be the same.
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 2:16pm

Thanks clayman2 and Leif for the responses.

Clayman2:  Couldn't get yours to work, but thanks for the input.

Leif: Yours works fine.  I appreciate it.

August 20th, 2015 2:17pm

It works cause the -gt operator is used instead of -eq
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:14pm

This should work:

$Today = [datetime]::Today
Get-ADOrganizationalUnit -Filter {whenCreated -gt $Today} -Properties whenCreated

  • Marked as answer by SdeDot Thursday, August 20, 2015 6:12 PM
August 20th, 2015 5:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics