Active Directory · Windows PowerShell 5.1
AD users locked out now, or with an expired password
Which enabled Active Directory user accounts are locked out right now, and which have a password that has expired?
Script
#Requires -Modules ActiveDirectory
# Enabled users that are locked out or whose password has expired. Read-only.
param(
[string]$Server = (Get-ADDomain).PDCEmulator
)
Get-ADUser -Server $Server -Filter 'Enabled -eq $true' -Properties LockedOut, PasswordExpired, PasswordLastSet, LastLogonDate |
Where-Object { $_.LockedOut -or $_.PasswordExpired } |
Sort-Object SamAccountName |
Select-Object SamAccountName, Name, LockedOut, PasswordExpired, PasswordLastSet, LastLogonDate, DistinguishedName
Checked automatically against Microsoft's published cmdlet reference. KBY hasn't run it in your tenant; run it in a test environment first.
Where to run it
A domain-joined machine with the Active Directory module (RSAT) installed.
Reading the output
- Uses the LockedOut and PasswordExpired properties of the AD module's account objects, which the module derives from userAccountControl and msDS-User-Account-Control-Computed (and, for LockedOut, lockoutTime). Each account is listed once, with both flags shown.
- The state is read from one domain controller: the PDC emulator of the current domain by default, or the one you pass with -Server.
- Disabled accounts are left out.
- To see where a lockout came from, use the account lockouts script, which reads event 4740 on a domain controller.
Sources
The Microsoft reference page for every cmdlet the script calls, plus the API and product pages behind each permission and caveat.
Get-ADDomain (ActiveDirectory)Get-ADUser (ActiveDirectory)about_ActiveDirectory_ObjectModel (LastLogonDate, PasswordLastSet and the account flag properties)about_ActiveDirectory_Filter (Filter and LDAPFilter, bitwise AND matching rule)ms-DS-User-Account-Control-Computed attributeWhere-Object (Microsoft.PowerShell.Core)Sort-Object (Microsoft.PowerShell.Utility)Select-Object (Microsoft.PowerShell.Utility)