Share via

Get-ADUser datetime time zone

Brian Wilson 21 Reputation points
2022-08-24T23:29:56.26+00:00

When trying to pull AD user account info using the below script I get different date/time stamps depending on the time zone of the server pulling the results. Is there a way to set the time zone as a variable in the script to make the results accurate, as defined to the time zone, regardless of the time zone of the server pulling the results.

$ExportPath = 'users_in_ou.csv'  
Get-ADUser -Properties * -Filter * |  Select `  
CN, @{Name='lastLogon'; Expression={if($_.lastLogon -ne $null -and $_.lastLogon -gt '0'){[datetime]::FromFileTime($_."lastLogon")}else{''}}} `  
, @{Name='lockoutTime'; Expression={if($_.lockoutTime -ne $null -and $_.lockoutTime -gt '0'){[datetime]::FromFileTime($_."lockoutTime")}else{''}}} `  
| export-csv $ExportPath -NoTypeInformation  
Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Microsoft 365 and Office | Development | Other
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2022-08-25T02:27:33.09+00:00

    FileTimes are kept in 64-bit integers. They don't have any time zone information. I think what you want to do is to use the FromFileTimeUtc method and avoid the conversion to local time.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.