简体   繁体   中英

Get Ad user Created date

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.

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 a date format that Get-Date , but [datetime]::FromFileTime() cannot handle it. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM