Hi All,
I was looking for a PowerShell script which will send password expiry email reminders to staff when their password is about to expire in 10days, 7days, 5days and 3days
I have the below script which reminds them when 4 days are left and then everyday until expiry date. Just wanted to update so it gets sent when 10days left, 7days left 5days left then 3days.
Import-Module ActiveDirectory
### Function to extract remaining Days ######
Get-ADUser -filter * -properties PasswordLastSet,EmailAddress,GivenName -SearchBase "DC=domain,DC=local" |foreach {
$PasswordSetDate=$_.PasswordLastSet
$maxPasswordAgeTimeSpan = $null
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
$today=get-date
$ExpiryDate=$passwordSetDate + $maxPasswordAgeTimeSpan
$daysleft=($ExpiryDate - $today)
$display=$daysleft.days
$UserName=$_.GivenName
if ($display -lt 5 -and $display -gt 1){
####### Email Message ###########
$MyVariable = @"
$UserName, your network password will expire in $display day(s)
To change your password, please follow the instructions below:-
For your password to be valid it must 12 or more characters long and contain a mix of THREE of the following FOUR properties:
- uppercase letters (A-Z)
- lowercase letters (a-z)
- numbers (0-9)
- symbols (!"?$%^&*)
*** This is automatically generated email - please do not reply ***
"@
send-mailmessage -to $_.EmailAddress -from administrator@domain.com -Subject "Password Expiry Reminder Notification" -body $MyVariable -smtpserver exchange server
}
}