Search code examples
windowspowershellldapldap-query

How to filter value 1/1/1601 1:00:00 AM of password last set?


I want to print out the password last set values by using "paswordLastSet" attribute. After implementing some filters as the following:

$passwordLS = $user.Properties.Item("pwdLastSet")[0]

if($passwordLS -eq 0)
    {         
         $value = "No password last set"
    }
    else
    {
         $value = [DateTime]::FromFileTime($passwordLS)
    } 

And I got an output:

No password last set
2/26/2003 12:21
11/27/2003 11:30
11/27/2003 11:30
1/1/1601 1:00:00 AM
.................

As listed above, there is a value "1/1/1601 1:00:00 AM" which means user never set their password. But, I do not want to take this value as my data. I would love to filter it out with "No password last set" instead of giving me "1/1/1601 1:00:00 AM" as an output.

I have investigated this issue to find an element that could help me somehow filter "1/1/1601 1:00:00 AM" , even I tried to print all the values by 100-nanoseconds to get the mutual value for this. Unfortunately, it's all different from each other.

I have also try to give a statement:

if($passwordLS -eq 0)
    {         
         $value = "No password last set"
    }
    else
    {
         $value = [DateTime]::FromFileTime($passwordLS)
         if($value -eq "1/1/1601 1:00:00 AM"){

                $value = "No password last set"    
         }
         else{    
                $value = $value
         }
    }    

But I think it's not a decent code. Any better experience ?


Solution

  • The reason this is not working is because you are comparing the datetime object $value to the string "1/1/1601 1:00:00 AM"

    You have many options - e.g:

    • Compare $value to [datetime] object - for example $("1/1/1601 01:00:00" | Get-Date)
    • Exclude based on year, without worrying about the reset of date information.

    The Else setting $value = $value is redundant.

    Code:

    if($passwordLS -eq 0)
        {         
             $value = "No password last set"
        }
        else
        {
             $value = [DateTime]::FromFileTime($passwordLS)
             if($value.Year -eq 1601){                           # this one
             if($value -eq $("1/1/1601 01:00:00" | Get-Date)){   # or this one
                    $value = "No password last set"    
             }
    
        }