/commands/active-directory/search-adaccount-inactive-accounts
Search-ADAccount: find inactive, disabled, expired and locked-out AD accounts
- tool
- Active Directory PowerShell
- versions
- ActiveDirectory module (RSAT) on Windows 10/11 and Windows Server 2016+
- os
- windows
Search-ADAccount finds user, computer and service accounts by state without writing a filter. The most used form lists accounts that have not logged on for a period:
Import-Module ActiveDirectory
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 -UsersOnly |
Select-Object Name, SamAccountName, LastLogonDate, Enabled |
Sort-Object LastLogonDate-TimeSpan 90.00:00:00 means 90 days (days.hours:minutes:seconds); -DateTime '2026-07-01' uses a fixed cutoff instead. -UsersOnly or -ComputersOnly restricts the object type; without either, both are returned.
Switches
| Switch | Finds |
|---|---|
| -AccountInactive -TimeSpan / -DateTime | accounts whose last logon is older than the period (or that never logged on) |
| -AccountDisabled | disabled accounts |
| -AccountExpired | accounts past their expiration date |
| -AccountExpiring -TimeSpan 30.00:00:00 | accounts that expire within the period |
| -LockedOut | accounts currently locked out |
| -PasswordExpired | accounts whose password has expired |
| -PasswordNeverExpires | accounts with the password-never-expires flag |
Common parameters: -SearchBase 'OU=Staff,DC=contoso,DC=com' to search one OU, -SearchScope OneLevel, -Server dc01.contoso.com, -ResultSetSize N.
Report, then act
$stale = Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 -UsersOnly |
Where-Object Enabled
$stale | Select-Object Name, SamAccountName, LastLogonDate, DistinguishedName |
Export-Csv stale-users.csv -NoTypeInformation
$stale | Disable-ADAccount -WhatIfReview the CSV and the -WhatIf output before running the last line without -WhatIf. Service accounts, break-glass accounts and accounts used only through other systems (such as cloud-only sign-ins synchronised from AD) can look inactive and still be needed.
Unlock and check locked-out users
Search-ADAccount -LockedOut -UsersOnly | Select-Object Name, SamAccountName, LastLogonDate
Unlock-ADAccount -Identity jdoe