Password expiration PowerShell script to notify Active Directory domain users
Many organizations enforce password expiration policies to reduce the risk of compromised credentials and unauthorized access. When users fail to change their passwords before expiration, they are locked out of their accounts.
To avoid account lockouts and help desk escalations, administrators often use an Active Directory (AD) password expiration notification PowerShell script to notify users before their passwords expire. A widely used script like password-expiration-notifications.ps1 script uses the Get-ADUser parameter to retrieve password expiration details from AD and SMTP to automatically email users reminders before expiration deadlines.
This guide explains how to:
Check AD password expiration dates using PowerShell.
Send password expiry notification emails.
Automate notification scripts using Task Scheduler.
Compare PowerShell-based notifications with ADSelfService Plus.
ManageEngine ADSelfService Plus also provides a built-in alternative for password expiration notifications through email, SMS, and push notifications without requiring manual scripting or script maintenance.
Step 1. Checking when AD passwords expire using PowerShell
Before sending password expiry notifications, administrators must first retrieve password expiration information from AD.
To retrieve all the AD user account properties related to password expiration, use this PowerShell script:
Get-ADUser -Filter * -Properties PasswordLastSet
Get-ADUser:
Retrieves one or more AD user objects based on a specified filter or identity.
-Filter *:
Required parameter that defines the search scope; the wildcard * returns all user accounts in the directory.
-Properties PasswordLastSet:
Specifies additional attributes to retrieve beyond the default set; PasswordLastSet returns the date and time the user's password was last changed.
To scope the query to a specific OU, you can use the SearchBase parameter:
Get-ADUser: Retrieves one or more AD user objects based on a specified filter or identity.
-Filter *: Required parameter that defines the search scope; the wildcard * returns all user accounts in the directory.
-SearchBase: Limits the search to a specific organizational unit (OU) or container in the directory tree.
-Properties PasswordLastSet: Specifies additional attributes to retrieve beyond the default set; PasswordLastSet returns the date and time the user's password was last changed.
To retrieve the domain-wide password expiration policy, use the following PowerShell script:
Get-ADUser: Retrieves AD user objects based on a specified filter.
-Filter {Enabled -eq $True -and PasswordNeverExpires -eq $False}: Returns only active users whose passwords are set to expire.
-Properties EmailAddress,msDS-UserPasswordExpiryTimeComputed: Retrieves additional attributes beyond the default set.
EmailAddress: The user's email address.
msDS-UserPasswordExpiryTimeComputed: The exact date and time the user's password will expire.
Handling fine-grained password policies in your script
Many organizations use fine-grained password policies (FGPPs) to apply different password expiration settings to specific users or groups.
Unlike the default domain password policy, FGPPs allow administrators to configure:
Different password expiration periods for different specific users or global security groups
Password complexity requirements
Password history requirements
For example:
IT admins may have shorter password expiration periods.
Service accounts may use different policies.
Executives may have stricter complexity rules.
FGPP overrides the domain-level MaxPasswordAge setting. Because of this, manually calculating password expiration dates using PasswordLastSet and MaxPasswordAge may produce inaccurate results.
The msDS-UserPasswordExpiryTimeComputed attribute automatically factors in FGPP, and is therefore the preferred approach for determining password expiration dates.
Automate password expiry calculation
Instead of manually calculating password expiration dates, use the following PowerShell script to retrieve all AD users and calculate password expiry:
The limitations of PowerShell-based password expiry notifications
Many admins rely on PowerShell scripts to automate password expiry notifications. While effective, these scripts often require ongoing maintenance and troubleshooting. They may stop working after PowerShell updates, SMTP configuration changes, or security policy modifications.
PowerShell-based notifications also offer limited visibility into whether emails were successfully delivered or acted upon. Missed password expiry notifications can lead to:
User lockouts
Increased help desk tickets
Remote access disruptions
Productivity loss
Compliance gaps
In larger environments, managing notification schedules, SMTP authentication, and exception handling across multiple scripts can become time-consuming and difficult to scale.
A smarter alternative: ADSelfService Plus
Instead of maintaining custom PowerShell scripts, organizations can use ADSelfService Plus to configure password expiration notifications through a centralized interface. To enable password expiry notifications using ADSelfService Plus:
Open the ADSelfService Plus admin portal.
Go to Configuration > Password Expiration Notification. In the Password/Account Expiration Notification section that opens, click Add New Notification.
Use the Select Domain option to specify the domain whose users should receive the notifications. Provide a Scheduler Name.
Set the Notification Type to Password Expiration Notification. Use the Notify via option to specify the notification medium (mail, SMS, or push notification).
Select Notification Frequency (Daily, Weekly, or On Specific Days) and use the Schedule Time option to specify the date and time of the notification delivery. For example, if you want to notify users seven days before the password expiration, select the On Specific Days option, click Schedule Time, and specify 7 in the field provided.
Edit the Subject and the Message of the notification, if required.
Click Advanced, and in the pop-up window that opens, use the options for excluding disabled users or smart card users from receiving expiration notifications and sending notification delivery status messages to users' managers or anyone with an admin account if necessary.