Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting LastLogon to DateTime format

My objective is to get a list of users from my domain with the following info:

-Display name -Country -Manager Name -Last login date

I am running the following script, and everything looks good except for the LastLogon. It outputs the time into a bunch of random numbers like "129948127853609000". How can I convert this to DateTime format?

Search-ADAccount -UsersOnly -SearchBase "OU=International,DC=mycompany,DC=com" -AccountDisabled:$false | Get-ADUser -Properties Name, manager, LastLogon | Select Name, manager, LastLogon | export-csv C:\Australia.csv -NoTypeInformation 
like image 725
Pickle Avatar asked Oct 26 '12 17:10

Pickle


People also ask

How is LastLogonTimeStamp calculated?

Whenever a user logs on, the value of this attribute is read from the DC. If the value is older [ current_time - msDS-LogonTimeSyncInterval ], the value is updated. The initial update after the raise of the domain functional level is calculated as 14 days minus random percentage of 5 days.

What is the difference between Lastlogon and LastLogonTimeStamp?

The main difference between lastlogon and lastLogonTimeStamp is that lastlogon is updated on the Domain Controller after the user interactive logon while lastLogonTimeStamp is replicated to all Domain Controller in AD Forest, the default value is 14 days. The Lastlogon attribute is not replicated.


2 Answers

DateTime.FromFileTime should do the trick:

PS C:\> [datetime]::FromFileTime(129948127853609000)  Monday, October 15, 2012 3:13:05 PM 

Then depending on how you want to format it, check out standard and custom datetime format strings.

PS C:\> [datetime]::FromFileTime(129948127853609000).ToString('d MMMM') 15 October PS C:\> [datetime]::FromFileTime(129948127853609000).ToString('g') 10/15/2012 3:13 PM 

If you want to integrate this into your one-liner, change your select statement to this:

... | Select Name, manager, @{N='LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}} | ... 
like image 112
latkin Avatar answered Oct 13 '22 21:10

latkin


Get-ADUser -Filter {Enabled -eq $true} -Properties Name,Manager,LastLogon |  Select-Object Name,Manager,@{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} 
like image 40
Shay Levy Avatar answered Oct 13 '22 22:10

Shay Levy