Get Password Last Set, Password Age and Expiry Date with PowerShell
Find when Active Directory users last changed their password, how old it is and when it expires, with PowerShell; list users whose passwords expire soon.
"When does my password expire?" is a frequent help desk question. The answer lives in two Active Directory attributes:
- pwdLastSet (
PasswordLastSetin PowerShell): when the password was last changed. - msDS-UserPasswordExpiryTimeComputed: the expiry time computed from the domain password policy and any applicable FGPP.
A single user
Get-ADUser ahmet.yilmaz -Properties PasswordLastSet, PasswordNeverExpires, "msDS-UserPasswordExpiryTimeComputed" |
Select-Object SamAccountName, PasswordLastSet, PasswordNeverExpires,
@{ n = 'Expires'; e = {
if ($_.PasswordNeverExpires) { 'Never' }
else { [datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed') } } }
Pitfall: for accounts whose password never expires,
msDS-UserPasswordExpiryTimeComputed returns a huge number and FromFileTime
throws. That is why the command checks PasswordNeverExpires first.
If PasswordLastSet is empty, the user must change the password at next sign-in
(pwdLastSet = 0).
Password age in days
Get-ADUser ahmet.yilmaz -Properties PasswordLastSet |
Select-Object SamAccountName, @{ n = 'AgeDays'; e = { (New-TimeSpan $_.PasswordLastSet (Get-Date)).Days } }
The domain's password lifetime
To see the maximum password age in the default domain policy:
Get-ADDefaultDomainPasswordPolicy | Select-Object MaxPasswordAge, MinPasswordAge, MinPasswordLength, PasswordHistoryCount
If an FGPP applies to a user, the effective values may differ:
Get-ADUserResultantPasswordPolicy -Identity ahmet.yilmaz
If it returns nothing, the default domain policy applies.
Users whose password expires within 14 days
$limit = (Get-Date).AddDays(14)
Get-ADUser -Filter 'Enabled -eq $true -and PasswordNeverExpires -eq $false' `
-Properties "msDS-UserPasswordExpiryTimeComputed", mail |
Where-Object { $_.'msDS-UserPasswordExpiryTimeComputed' -gt 0 } |
Select-Object SamAccountName, mail,
@{ n = 'Expires'; e = { [datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed') } } |
Where-Object { $_.Expires -le $limit -and $_.Expires -ge (Get-Date) } |
Sort-Object Expires
Export the result with Export-Csv. A ready-made script that emails the list is on
the free tools page.
The cost of an expired password
Users usually notice an expired password when they switch on their computer in the morning; if they are remote or cannot connect to the VPN, they call the help desk. A self-service portal lets them reset it themselves with a code sent to their registered phone. The PRESS admin panel also has a report of accounts whose password never expires.
Evaluate PRESS in your own environment.
Through the live demo you can explore the user portal and the admin panel directly.