Export last logon time for users in Active Directory

Perhaps time to clean up your Active Directory? Need to check when your users last logged on?

Get-ADUser -Credential abc.local\Administrator  `
-server servername.abc.local `
-Filter {(employeeID -like "*") -And (Enabled -eq $true)}  `
-Properties LastLogonTimeStamp |
Select-Object Name,@{Name="LastLogonTimeStamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd-HH:mm:ss')}} 
| Export-Csv -Force -Path $env:USERPROFILE\Desktop\$(get-date -f yyyy-MM-dd-HH-mm-ss)-LastLogonTime.csv -NoTypeInformation -Delimiter ";" -Encoding UTF8

The time format here could probably be adjusted for easier sorting in Excel, but I have not had the time for that yet.

If you just want a “one-liner” to check on a single user you can use:

Get-ADUser -Credential abc.local\Administrator -server servername.abc.local -Identity "USERNAME" -Properties "LastLogonDate" | select LastLogonDate

Now you can tell your users to logon or gtfo 😂

Leave a Reply

Your email address will not be published. Required fields are marked *