Reputation: 15
I'm trying to export various user data, but I'm having trouble with the "created date" I'm using:
Get-ADUser -Filter * –Properties DisplayName, givenName, sn, SamAccountName, Enabled, lastLogonTimestamp, whenCreated |
Select DisplayName, givenName, sn, SamAccountName, Enabled,
@{n="lastLogonDate";e={get-date ([datetime]::FromFileTime($_.lastLogonTimestamp)) -f MM/dd/yyyy}},
@{n="Created";e={get-date ([datetime]::FromFileTime($_.whenCreated)) -f MM/dd/yyyy}}|
Export-CSV -NoType .\usrtst01.csv
But,The export goes like this:
"User test","User","test","user.test","True","04/01/2020",
However, if I use the code like this:
Get-ADUser -Filter * –Properties DisplayName, givenName, sn, SamAccountName, Enabled, lastLogonTimestamp, whenCreated |
Select DisplayName, givenName, sn, SamAccountName, Enabled,
@{n="lastLogonDate";e={get-date ([datetime]::FromFileTime($_.lastLogonTimestamp)) -f MM/dd/yyyy}}, whenCreated |
Export-CSV -NoType .\usrtst02.csv
The export:
"User test","User","test","user.test","True","04/01/2020","01/04/2020 8:28:31 AM"
The problem is that I need the format:
MM/dd/yyyy
Could you helpme please.
Thanks.
Upvotes: 0
Views: 11343
Reputation: 3154
lastLogonTimestamp
and whenCreated
have are two different data types.
lastLogonTimestamp
is represented as a large Integer, counting the ticks from 1.1.1601, hence you must convert it. You are doing that with [datetime]::FromFileTime()
.
whenCreated
in contrast is of format datetime
that Get-Date
can handle, but [datetime]::FromFileTime()
cannot. If you invoke just that line, that doesn't work for you as expected you get a proper error.
get-date ([datetime]::FromFileTime("01/04/2020 8:28:31 AM")) -f MM/dd/yyyy
Cannot convert argument "fileTime", with value: "01/04/2020 8:28:31 AM", for "FromFileTime" to type "System.Int64": "Cannot convert value "01/04/2020 8:28:31 AM" to
type "System.Int64". Error: "Input string was not in a correct format.""
That's exactly what's breaking your command and leaving the output string blank.
Simply remove [datetime]::FromFileTime()
and you're good to go.
get-date ("01/04/2020 8:28:31 AM") -f MM/dd/yyyy
Upvotes: 4