Finding time difference

I’m trying to take Active Directory LastLogonTimeStamp and display the time difference from the current date. In the end, I want to display in days since the account was used.

This is what I tried but the numbers are wrong

$Last = Get-ADUser -Identity username -Properties LastLogonTimeStamp| select LastLogonTimeStamp -ExpandProperty LastLogonTimeStamp

$current = Get-Date

New-TimeSpan -start $current -end $last

The dates are backwards, so you would get negative values.

New-TimeSpan -start $last -end (Get-Date)

If that isn’t the issue, “the numbers are wrong” doesn’t explain what you’re seeing or expected results. Please provide specifics.

I changed the values and this is what I get

PS C:\WINDOWS\system32> New-TimeSpan -start $last -end $currect

The days are wrong as this account lastlogontimestamp is 6/6/2019

Days : 584395
Hours : 4
Minutes : 43
Seconds : 6
Milliseconds : 22
Ticks : 504917449860222822
TotalDays : 584395.19659748
TotalHours : 14025484.7183395
TotalMinutes : 841529083.100371
TotalSeconds : 50491744986.0223
TotalMilliseconds : 50491744986022.3

The type you get from the cmdlet Get-ADUser is barely a datetime. So you have to convert it first before you can use it …

$Last = Get-ADUser -Identity username -Properties LastLogonTimeStamp | select LastLogonTimeStamp -ExpandProperty LastLogonTimeStamp
$current = Get-Date
New-TimeSpan -start current -end ( [datetime]::FromFileTime($last) )

I believe lastlogondate is the same thing already in datetime format.

$a = get-aduser js -property lastlogondate
(get-date) - $a.lastlogondate

Days              : 14
Hours             : 23
Minutes           : 51
Seconds           : 42
Milliseconds      : 422
Ticks             : 12955024225006
TotalDays         : 14.9942410011644
TotalHours        : 359.861784027944
TotalMinutes      : 21591.7070416767
TotalSeconds      : 1295502.4225006
TotalMilliseconds : 1295502422.5006

Understanding the AD Account attributes - LastLogon, LastLogonTimeStamp and LastLogonDate


I have it worked but how can I get the Days inactive to just display days and not all the other numbers


$getuser = Get-ADUser -Identity user-Properties LastLogonDate| select LastLogondate,name

$Properties = @{Name = $
LastLogonTime = $getuser.lastlogondate
'Days Inactive' = (get-date) - $getuser.LastLogondate

$Obj = New-Object -TypeName PSObject -Property $Properties | Select name, LastLogonTime ,'Days Inactive'


Name LastLogonTime Days Inactive

user 6/6/2019 1:07:24 AM 7.13:57:40.2365427

I was going to say you can drop the last select, but I see how it changes the order.

I have it worked but how can I get the Days inactive to just display days and not all the other numbers
'Days Inactive' = ((get-date) - $getuser.LastLogondate).days


'Days Inactive' = (get-date) - $getuser.LastLogondate | Select-Object -ExpandProperty Days

Edit: by the way, you really should read the link that Olaf posted. There are important differences with these attributes that could affect the accuracy of your reporting.