Need to find out when a user last logged in? PowerShell gives you fast, accurate results directly from Active Directory — whether you need a single account or a full domain export.
Before running any command, it's important to understand the difference between two commonly confused AD attributes:
LastLogonDate
lastLogon
Use LastLogonDate for most reporting tasks. It is replicated across all domain controllers and returns a clean, human-readable timestamp. Use lastLogon only when you need the exact login time from a specific DC and are prepared to query every DC in the domain.
RSAT-AD-PowerShell
The simplest and most reliable approach. Replace jsmith with the target user's SamAccountName:
jsmith
Get-ADUser -Identity "jsmith" -Properties LastLogonDate | Select-Object Name, SamAccountName, LastLogonDate
Sample output:
Name SamAccountName LastLogonDate ---- -------------- ------------- John Smith jsmith 6/28/2026 9:14:03 AM
Use this when you need the exact login time. Because lastLogon is not replicated, you must query every domain controller and find the most recent value. The raw value is a Windows FileTime integer and must be converted:
$user = "jsmith" $latestLogin = $null Get-ADDomainController -Filter * | ForEach-Object { $dc = $_.HostName $result = Get-ADUser -Identity $user -Properties lastLogon -Server $dc $loginTime = [DateTime]::FromFileTime($result.lastLogon) if ($loginTime -gt $latestLogin) { $latestLogin = $loginTime } } Write-Host "Most recent login for $user`: $latestLogin"
Useful for audits, license reviews, or identifying stale accounts. This queries the entire domain and exports to a CSV file in your current directory:
Get-ADUser -Filter * -Properties LastLogonDate, Enabled | Select-Object Name, SamAccountName, Enabled, LastLogonDate | Sort-Object LastLogonDate | Export-Csv -Path ".\AD_LastLogins.csv" -NoTypeInformation Write-Host "Export complete: AD_LastLogins.csv"
To filter for accounts that haven't logged in within the last 90 days (useful for finding stale or inactive accounts):
$cutoff = (Get-Date).AddDays(-90) Get-ADUser -Filter {LastLogonDate -lt $cutoff -and Enabled -eq $true} -Properties LastLogonDate | Select-Object Name, SamAccountName, LastLogonDate | Sort-Object LastLogonDate | Export-Csv -Path ".\Stale_Accounts.csv" -NoTypeInformation Write-Host "Stale account export complete: Stale_Accounts.csv"
-SearchBase "OU=Users,DC=contoso,DC=com"
When using any software or code, it is important to remember that there is always a certain level of risk involved. As a user, you are responsible for ensuring that the software or code you are using is suitable for your needs and that you are aware of any potential risks associated with its use.
If you are considering using any code or software provided by ITMS, we advise you to review our terms of service at https://www.itms-us.com/Website-Terms-Of-Use.
Any code provided by ITMS is provided "as is," without warranties or guarantees. By using our code, you acknowledge and accept the risks associated with its use and agree to hold ITMS harmless for any damages or losses that may result.
IT Master Services provides Managed IT and consulting services that keep your AD environment clean, secure, and auditable.