PowerShell script to display AD user's password expiration dates

Please provide me with a Server 2016 PowerShell script to display each Active Directory user's password expiration date.

I need to run this command that will display a list of all Active Directory users and their AD password expiration dates without having to run this command on each individual AD account.
IT GuyNetwork EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This should do the trick:
Get-ADUser -Filter {(Enabled -eq $true) -and (PasswordNeverExpires -eq $false)} –Properties 'msDS-UserPasswordExpiryTimeComputed' |
	Select-Object -Property SamAccountName, @{n='ExpiryDate'; e={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}}

Open in new window

With export:
Get-ADUser -Filter {(Enabled -eq $true) -and (PasswordNeverExpires -eq $false)} –Properties 'msDS-UserPasswordExpiryTimeComputed' |
	Select-Object -Property SamAccountName, @{n='ExpiryDate'; e={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}} |
	Export-Csv -NoTypeInformation -Path C:\Temp\PWExpiry.csv

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT GuyNetwork EngineerAuthor Commented:
These scripts both work very well; however they only display this info for the users who don't have the "Password never expires" checkbox selected.

What is the syntax for running this script so that this information will be displayed for all users who do have the "Password never expires" checkbox selected so I will know when these user's AD passwords will expire once the "Password never expires" checkbox is deselected?

I need to determine exactly when user's passwords will be expiring (or if they already have) before I deselect the AD "Password never expires" checkbox  for those users who have it selected.
That's a bit more involved, since the msDS-UserPasswordExpiryTimeComputed for the obvious reasons doesn't get computed if the password doesn't expire, and the fine grained password policies have to be taken into account, but this should do the trick:
$DDPP = Get-ADDefaultDomainPasswordPolicy
$Now = Get-Date
Get-ADUser -Filter {(Enabled -eq $true) -and (PasswordNeverExpires -eq $true)} -Properties PasswordLastSet | ForEach-Object {
	$PP = Get-ADUserResultantPasswordPolicy -Identity $_ -ErrorAction SilentlyContinue
	If (-not $PP) {$PP = $DDPP}
		'SamAccountName' = $_.SamAccountName
		'PasswordNeverExpires' = $true	## Just to avoid confusion about the expiry time
		'PasswordExpiryTime' = If ($PP.MaxPasswordAge -eq [timespan]::Zero) {'<Never>'} Else {$_.PasswordLastSet + $PP.MaxPasswordAge}
		'PasswordExpired' = If ($PP.MaxPasswordAge -eq [timespan]::Zero) {$false} Else {If (($_.PasswordLastSet + $PP.MaxPasswordAge) -gt $Now) {$false} Else {$true}}

Open in new window

Naveen SharmaCommented:
One-Liner: Get a List of AD Users Password Expiry Dates:

Lepide User Password Expiration Reminder an intuitive console giving you a complete overview of password status: https://www.lepide.com/lepideauditor/password-expiration-notification.html

PowerShell Script:
Password Expiration Report
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.