Check a User's Last Login with PowerShell

Active Directory | Export to CSV

Last Login in Active Directory

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:

Attribute Replicated? Accuracy Best For
LastLogonDate Yes Within ~14 days Most reporting scenarios
lastLogon No: per DC only Exact (on that DC) Precise single-DC audits

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.

Check a user's last login in Active Directory with PowerShell
Requires: Active Directory PowerShell module (RSAT-AD-PowerShell) and Domain Admin or equivalent read rights.

Method 1: Single User Using LastLogonDate Recommended

The simplest and most reliable approach. Replace jsmith with the target user's SamAccountName:

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

Method 2: Single User Using lastLogon (Per DC)

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"

Method 3: Export All Users' Last Login to CSV

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"
Pro Tips
  • Run these commands from a machine with the Remote Server Administration Tools (RSAT) installed, or directly on a Domain Controller.
  • LastLogonDate replicates on a ~14-day cycle by default. A user who logged in yesterday may show a date up to 14 days old, this is normal.
  • Never disable an account solely based on LastLogonDate without also checking lastLogon across all DCs to confirm inactivity.
  • Add -SearchBase "OU=Users,DC=contoso,DC=com" to scope your query to a specific Organizational Unit instead of the entire domain.

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.

Need Help Managing Your Active Directory?

IT Master Services provides Managed IT and consulting services that keep your AD environment clean, secure, and auditable.

Managed IT Services