Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Last active June 1, 2018 14:15
Show Gist options
  • Select an option

  • Save JohnLBevan/8562dfda661d0d375d24b2cf87aef7d3 to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/8562dfda661d0d375d24b2cf87aef7d3 to your computer and use it in GitHub Desktop.
Calculate LDAP DateTime from FileTime
#Get-aduser myUsername -Properties lastlogondate, lastlogon, lastlogontimestamp | select lastlogondate, lastlogon, lastlogontimestamp
#LastLogonDate is a DateTime
#LastLogon is a Long / Int64
#LastLogonTimestamp is a Long / Int64
#NB: values may differ slightly, as LastLogonDate/LastLogonTimestamp is cached for up to 2 weeks before replicating to all servers, whilst LastLogon is updated instantly but only on the users logon server: https://serverfault.com/a/734641/137255
$lastLogonTimeLdap = 131723288263417819
#The official way
[DateTime]::FromFileTimeUtc($lastLogonTimeLdap)
#01 June 2018 12:13:46
#The DIY way (useful to know if you need to replicate in other languages)
[DateTime]::MinValue.AddYears(1600).AddMilliseconds(($lastLogonTimeLdap / 10000))
#01 June 2018 12:13:46
# NB: ([DateTime]::MinValue.AddYears(1600) = 1601-01-01 00:00:00)
# 1601-01-01 00:00:00 = Win32's Epoch
# See https://blogs.msdn.microsoft.com/oldnewthing/20090306-00/?p=18913 for explanation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment