How to Detect Last Logon Date and Time for All Active Directory Users

Authored by: Support.com Tech Pro Team

1. Introduction

How to Detect Last Logon Date and Time for All Active Directory Users

 

2. Detecting Last Logon Time with PowerShell

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: