Authored by: Support.com Tech Pro Team
How to Detect Last Logon Date and Time for All Active Directory Users
Start Windows PowerShell through the Start Menu or by using “Runâ€. You can also type “PowerShell†in the Start Menu search and press “Enterâ€.
Copy and run the following script to generate the last logon reports on the command screen:
Import-Module ActiveDirectoryfunction Get-LastLogonEvents{$dcs = Get-ADDomainController -Filter {Name -like "*"}$users = Get-ADUser -Filter *$time = 0foreach($user in $users){foreach($dc in $dcs){ $hostname = $dc.HostName$currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogonif($currentUser.LastLogon -gt $time) {$time = $currentUser.LastLogon}$dt = [DateTime]::FromFileTime($time)Write-Host $currentUser "last logged on at:" $dt$time = 0}}}Get-LastLogonEvents
Press the “Enter†key once at the end of the script to execute it.
It shows the following output on the screen: