lotirthos227
lotirthos227

Reputation: 327

Powershell - How do I get lastlogon convert into number of days

I want to query the Active directory to return me the number of days a user had login. For the moment I can get the lastlogon day like 2/26/2012 9:28:28 AM but I want to have the # of days between that lastlogon and today.

Here's the script I found on an other post on stackoverflow

Search-ADAccount -UsersOnly -SearchBase "OU=users,OU=City,dc=mydomain,DC=local" -AccountDisabled:$false | Get-ADUser -Properties Name, manager, LastLogon | Select Name, manager, @{N='LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}} | export-csv u:\Australia.csv -Delimiter ";" -NoTypeInformation

I would like to have an other column in my CSV call "Last Logon Days" in addition of the "lastlogon" that return me a integer telling me 50, which mean lastlogon was 50 days ago.

Update:

I used this command according to @Musaab Al-Okaidi reply

Search-ADAccount -UsersOnly  -AccountDisabled:$false | Get-ADUser -Properties Name, manager, LastLogontimestamp | select Name, @{manager,@{N='LastLogontimestamp'; E={[DateTime]::FromFileTime($_.LastLogontimestamp)}}, @{N='Last Logon Days'; E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogontimestamp))).Days}}

Upvotes: 2

Views: 10799

Answers (1)

Musaab Al-Okaidi
Musaab Al-Okaidi

Reputation: 3784

UPDATE: This will get you the number of days between the last logon and the day the script is run on.

Search-ADAccount -UsersOnly  -AccountDisabled:$false | Get-ADUser -Properties Name, manager, LastLogon | select Name, manager,@{N='LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}}, @{N='Last Logon Days'; E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}}

Upvotes: 4

Related Questions