Email notifications for Active Directory password expiry with a PowerShell script

#Articles#Entra ID#M365#Intune#VPN


Passwords are a constant headache. This is an update on password expiration in Active Directory and on how to warn users when a change is still required.

After many years in systems administration and cybersecurity, I have seen how policy changes affect both productivity and security.

Microsoft no longer recommends periodic password expiration. It dropped it from the Windows security baseline in May 2019 (Windows 10 v1903 and Windows Server v1903) and explains why in its security blog (source). The Microsoft 365 guidance points the same way: for cloud-only accounts, the recommendation is that passwords never expire (source). NIST is blunter: verifiers must not require periodic changes, and must force a change when there is evidence of compromise (SP 800-63B-4, section 3.1.1.2).

Still, many organizations keep expiration, because of a contract, an audit or plain inertia. If that is your case, a good warning system prevents incidents. Here is how to notify users with a Group Policy setting or a PowerShell script.

Picture an ordinary day:
One morning the IT team gets a flood of calls. Dozens of remote users cannot get in through the VPN. Their passwords expired without warning and, because they are not connected to Active Directory (AD), they have no way to change them. The result: hours of frustration, lost productivity and a help desk buried in reset requests.

This happens in environments with forced expiration, where VPN users do not always get the warning in time.

The fix is simple: automated expiry notices.

A PowerShell script that emails users a few days before their password expires lets them change it themselves, with no interruption.

Why did Microsoft drop periodic expiration?

According to its own blog, periodic expiration only defends against the chance that a password (or its hash) is stolen during its validity period. If it is never stolen, there is no need to expire it; and if there is evidence of theft, you act immediately instead of waiting for expiry. Microsoft calls it an ancient, very low-value mitigation and points to better alternatives: banned-password lists and multifactor authentication. It also says that forcing frequent changes brings its own problems: users often make a small, predictable tweak to the old password, or forget the new one.

In practice that means:

  • More predictable passwords: small variations of the previous one, or sequential numbers.
  • More typing errors and lockouts: frequent changes raise the number of mistakes and, with them, account lockouts.

What exactly changed?

A common misunderstanding needs clearing up. Microsoft says it explicitly: it only removes periodic expiration from its baseline. Minimum length, history and complexity requirements are unchanged. AD DS password policies still exist and are still configurable. What the baseline drops is the recommended expiration value, so each organization can choose its own without contradicting the guidance.

Microsoft also warns that the baseline is not a complete password strategy. For what it does recommend, banned-password lists and MFA, you have to configure them separately.

Where do you configure it?

It depends on the environment:

  • Active Directory Domain Services (AD DS): expiration, length, history and complexity are set in the domain password policy, or in fine-grained password policies (PSOs) for specific groups.
  • Microsoft 365: for cloud-only accounts, the only things you can change are how many days until a password expires, or whether it expires at all.
  • Microsoft Entra ID, banned-password protection: in the Microsoft Entra admin center, under Entra ID > Authentication methods > Password protection. It offers a global and a custom banned-password list, and can extend to on-premises AD DS through agents on the domain controllers (source). Microsoft recommends starting in audit mode and moving to enforced mode later.

What to keep in mind

  • Recent changes: Microsoft updates its services often, so menus may differ.
  • Legacy policies: local or Group Policy settings may affect how passwords behave.
  • Tooling: use what fits your environment. PowerShell is handy for changes at scale.

What this means for you

  • More room: you can favor long, memorable passwords without forcing changes.
  • Less friction: without frequent changes, fewer typing errors and fewer lockouts.
  • More weight on MFA: Microsoft recommends enforcing MFA registration and identity protection risk policies, plus a 14-character minimum (source).

Without periodic expiration, the pressure falls on long passwords, banned lists, MFA and an immediate change when compromise is suspected.

Notifying VPN users

Whatever the reason for a password change, it helps to warn users beforehand. It matters most for people who work remotely and connect over VPN.

When a password has expired, sign-in with cached credentials can fail, especially if the VPN client cannot change the AD password. Ideally users can reset it from a self-service portal. If you do not have one, you can warn them through Group Policy.

Another option is to send an email with a PowerShell script.

Warning users with Group Policy

Group Policy offers this setting to warn about an upcoming password change:

  • Interactive logon: Prompt user to change password before expiration

You will find it under Computer Configuration > Policies > Windows Settings > Security Settings > Local Policies > Security Options.

You can set a warning period from 0 to 999 days (source). Microsoft recommends five days. With 0, there is no warning at logon. The message only appears after the user signs in.

Users who have not connected to AD for a while may miss these warnings.

Emailing users with PowerShell

A PowerShell script can find the users whose passwords expire within a given window and email them, so they can connect to the domain and change it in time.

Get-ADUser filters accounts that require a password and whose password expires. The script then reads the msDS-UserPasswordExpiryTimeComputed attribute, which stores the expiry date as the number of 100-nanosecond intervals since 1 January 1601. To get the notification date, we convert that value to a date and subtract the number of days we want.

# Notify X days before expiry
$DaysBefore = 5

Get-ADUser -Filter 'PasswordNeverExpires -eq $false -and PasswordNotRequired -eq $false' `
    -Properties msDS-UserPasswordExpiryTimeComputed, Mail, PasswordLastSet |
Where-Object {
    $_."msDS-UserPasswordExpiryTimeComputed" -ne $null -and
    ([datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")).Date -eq (Get-Date).AddDays($DaysBefore).Date
} | ForEach-Object {
    Send-MailMessage -To $_.Mail -From "Admin <admin@acme.com>" `
        -Subject "Your password is expiring" `
        -Body "Your password will expire in $DaysBefore days. Please change your password as soon as possible!" `
        -SmtpServer smtp.acme.com
}

The script emails every account whose password expires within the window. To run it once a day, set it up as a scheduled task.

A warning about Send-MailMessage: Microsoft marks it as obsolete and says it does not guarantee secure connections to the SMTP server. There is no direct replacement in PowerShell; note DE0005 suggests the MailKit library and, with Exchange Online, Send-MgUserMail from the Microsoft Graph PowerShell SDK (source). Treat the example as a starting point and consider changing the sending method before production. The example values (admin@acme.com, smtp.acme.com) are fictional.

Summary

Periodic password changes are no longer recommended, but if your organization still enforces them, warning users in advance cuts support requests.

Group Policy is the simple option: it shows a warning to users who sign in. A PowerShell script is more flexible, because it emails users even when their computer is not connected to a domain controller.

How do you handle password expiry in your organization?

I would like to hear how you approach it, especially with remote or VPN-dependent users. Do you use any automation to warn them? Share your experience in the comments.

See you on social media.