Account Password age

$360Days = (Get-Date).adddays(-360)
$users = Get-ADUser -SearchBase “OU=XX,DC=com” -Filter {(passwordlastset -le $360Days) -AND (enabled -eq $false)} -properties passwordlastset
$users | %{
$pwdset = [DateTime]::FromFileTime($_.passwordlastset)
}

Im trying to change the date format of “passwordlastset” attribute using [DateTime]::FromFileTime, so that i can find the password age in numbers by finding the difference between (get-date)-$pwdset

But when i execute the above script im getting below mentioned error in powershell

Cannot convert argument “fileTime”, with value: “7/8/2010 12:23:53 PM”, for “FromFileTime” to type “System.Int64”: “Cannot convert value
“7/8/2010 12:23:53 PM” to type “System.Int64”. Error: “Invalid cast from ‘DateTime’ to ‘Int64’.””
At C:\Users\gssnckhi\AppData\Local\Temp\1c8afcfe-b440-47da-a419-9b6d996b13bf.ps1:11 char:1

  • $pwdset = [DateTime]::FromFileTime($_.passwordlastset)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
    
    

Cannot convert argument “fileTime”, with value: “7/5/2012 7:55:53 PM”, for “FromFileTime” to type “System.Int64”: “Cannot convert value
“7/5/2012 7:55:53 PM” to type “System.Int64”. Error: “Invalid cast from ‘DateTime’ to ‘Int64’.””
At C:\Users\gssnckhi\AppData\Local\Temp\1c8afcfe-b440-47da-a419-9b6d996b13bf.ps1:11 char:1

  • $pwdset = [DateTime]::FromFileTime($_.passwordlastset)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
    
    

Cannot convert argument “fileTime”, with value: “7/5/2012 8:01:19 PM”, for “FromFileTime” to type “System.Int64”: “Cannot convert value
“7/5/2012 8:01:19 PM” to type “System.Int64”. Error: “Invalid cast from ‘DateTime’ to ‘Int64’.””
At C:\Users\gssnckhi\AppData\Local\Temp\1c8afcfe-b440-47da-a419-9b6d996b13bf.ps1:11 char:1

  • $pwdset = [DateTime]::FromFileTime($_.passwordlastset)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
    
    

Cannot convert argument “fileTime”, with value: “7/5/2012 8:02:32 PM”, for “FromFileTime” to type “System.Int64”: “Cannot convert value
“7/5/2012 8:02:32 PM” to type “System.Int64”. Error: “Invalid cast from ‘DateTime’ to ‘Int64’.””
At C:\Users\gssnckhi\AppData\Local\Temp\1c8afcfe-b440-47da-a419-9b6d996b13bf.ps1:11 char:1

  • $pwdset = [DateTime]::FromFileTime($_.passwordlastset)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

PasswordLastSet is one of the properties that PowerShell has created for you, by converting the “pwdLastSet” LDAP attribute into a DateTime object. No need to call FromFileTime again; that’s already been done.

Any reason for 360 days rather than 1 year?

(get-date).AddYears(-1)

or

(get-date).AddMonths(-12)