This is a text-only version of the following page on https://raymii.org: --- Title : Microsoft Exchange / Active Directory Powershell script to notify users of expiring Passwords Author : Remy van Elst Date : 08-08-2013 URL : https://raymii.org/s/software/Microsoft_Exchange_Powershell_Script_User_Password_Expiry.html Format : Markdown/HTML --- This is a small PowerShell script which emails your users that their password is going to expire in X days. This is needed when you have an Active Directory and Exchange Environment, but your users do not log in to a Windows machine bound to the Active Directory, but for example a Mac OS X or Linux machine with Full Disk Encryption enabled. Then they are not notified that their password is about to expire. This script can run as a scheduled task and scan and email your users that their password is about to expire.
It is tested on a Windows Server 2008 environment, with Exchange 2010. Domain is not running in Mixed mode, all servers are Windows Server 2008 R2. The script is installed as a scheduled task on one of the Exchange Edge servers with the SMTP role, but it does not have to be there, because the SMTP server variable is configurable. The amount of days to email before a password expires is also configurable. ### PowerShell setup First we need to allow execution of unsigned Powershell scripts. _This can be dangerous, so make sure your server security is adequate._ Fire up a `cmd.exe` _with elevated privileges, run it as admin_ and launch `powershell`. Then execute the following command: Set-ExecutionPolicy unrestricted This does the following: Load all configuration files and run all scripts. If you run an unsigned script that was downloaded from the internet, you are prompted for permission before it runs. [More info on the Set-ExecutionPolicy cmdlet][2] ### The script $ExpireDays = 30 $SendingEmail = "helpdesk@example.org" $SMTPHost="127.0.0.1" Import-Module ActiveDirectory $AllUsers = get-aduser -filter * -properties * |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false } foreach ($User in $AllUsers) { $Name = (Get-ADUser $User | foreach { $_.Name}) $Email = $User.emailaddress $PasswdSetDate = (get-aduser $User -properties * | foreach { $_.PasswordLastSet }) $MaxPasswdAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge $ExpireDate = $PasswdSetDate + $MaxPasswdAge $Today = (get-date) $DaysToExpire = (New-TimeSpan -Start $Today -End $ExpireDate).Days $EmailSubject="Password Expiry Notice - your password expires in $DaystoExpire days" $Message=" Dear $Name,
Your Password expires in $DaysToExpire days.
To change your password, please go to https://webmail.example.org/owa/, log in and click the settings button, then click Change Password.
If you do not update your password in $DaysToExpire days, you will not be able to log in, so please make sure you update your password.
If you need any help, contact us via email: helpdesk@example.org, by internal phone 1337, or walk by Building C, Floor 4, Room C41A.
Sincerely,
The IT Department.