Password Expiration Notification Email
This script will email a user in the event that their password is due to expire in X number of days.
You have to modify some contents in below mentioned PowerShell Script_with your environment details.
- From Email Address
- # of days before notification send to users
- Your SMTP Address
- Also you can add/modify the mail body contents as per your requirements.
Import-Module ActiveDirectory;
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
Get-ADUser -filter * -properties PasswordLastSet, PasswordExpired, PasswordNeverExpires, EmailAddress, GivenName | foreach {
$today=get-date
$UserName=$_.Name
$Email=$_.EmailAddressif (!$_.PasswordExpired -and !$_.PasswordNeverExpires) {
$ExpiryDate=$_.PasswordLastSet + $maxPasswordAgeTimeSpan
$DaysLeft=($ExpiryDate-$today).daysif ($DaysLeft -lt 7 -and $DaysLeft -gt 0){
$WarnMsg = ”
<p style=’font-family:calibri’>Hi $UserName,</p>
<p style=’font-family:calibri’>Your Windows login password will expire in $DaysLeft days, please press CTRL-ALT-DEL and change your password. As a reminder, you will have to enter your new password into your Wi-Fi connected mobile device if prompted.</p><p style=’font-family:calibri’>Requirements for the password are as follows:</p>
<ul style=’font-family:calibri’>
<li>Must not contain the user’s account name or parts of the user’s full name that exceed two consecutive characters</li>
<li>Must not be one of your last 3 passwords</li>
<li>Contain characters from three of the following four categories:</li>
<li>English uppercase characters (A through Z)</li>
<li>English lowercase characters (a through z)</li>
<li>Base 10 digits (0 through 9)</li>
<li>Non-alphabetic characters (for example, !, $, #, %)</li>
</ul>
<p style=’font-family:calibri’>For any assistance, call to [Company Name] IT Team</a></p>”
ForEach ($email in $_.EmailAddress) {
send-mailmessage -to $email -from admin@mifarook.com -Subject “Password Reminder: Your password will expire in $DaysLeft days” -body $WarnMsg -smtpserver “smtp.mifarook.com” -BodyAsHtml }}
}
}