Trying to understand what is going on in this bit of code.

Thank you for taking a look.

I have a script that grabs disabled AD users and sorts them by last logon date. The output is an HTML table. That works fine. I have an additional script i found that will color a cell or row based on a filter and it works as long as I am not using dates. Looking into it more that script is okay but PS is doing something odd. Please look at the code below. I’d like to know why dates with the months January and October create a false condition.

Thank you for your help,

Paul

PS X:\> (Get-Date).AddDays(-30)

Sunday, October 18, 2020 3:48:09 PM

PS X:\> "The condition is " + (&{If("2/2/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("1/2/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("1/31/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("11/31/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("1/31/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("1/31/2019" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("1/31/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("1/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("2/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("3/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("4/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("5/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("6/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("7/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("8/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("9/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("10/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is False.
PS X:\> "The condition is " + (&{If("11/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\> "The condition is " + (&{If("12/1/2019 2:11:09 PM" -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."
The condition is True.
PS X:\>

Wrapping a date in qoutes is not automatically parsed to datetime type, you need to parse it to a date:

PS C:\Users\rasim> ("12/1/2019 2:11:09 PM").GetType() 


IsPublic IsSerial Name                                     BaseType     
-------- -------- ----                                     --------     
True     True     String                                   System.Object

PS C:\Users\rasim> (Get-Date -Date "12/1/2019 2:11:09 PM").GetType()


IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

Recommend wrapping the parse in parenthesis to ensure that it is parsed before the comparison:

"The condition is " + (&{If((Get-Date -Date "12/1/2019 2:11:09 PM") -gt (Get-Date).AddDays(-30)) {"True"} Else {"False"}}) + "."

Another option is to use New-TimeSpan to get the number of days versus just doing a basic boolean check.

New-TimeSpan -Start (Get-Date -Date "12/1/2019 2:11:09 PM") -End (Get-Date).AddDays(-30)

Thank you Rob, I thought I was going crazy. Much appreciate.