################################################# # Please Configure the following variables…. # expireindays1 + 2 = At what count of days left on a password do you want a notification? $smtpServer=”smtp.yourmailserver.com” $expireindays1 = 7 $expireindays2 = 1 $from = “Name ” ################################################# #Get Users From AD who are enabled Import-Module ActiveDirectory $users = get-aduser -filter * -Properties enabled, passwordneverexpires, passwordexpired, emailaddress, passwordlastset |where {$_.Enabled -eq “True”} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false } foreach ($user in $users) { $Name = (Get-ADUser $user | foreach { $_.Name}) $emailaddress = $user.emailaddress $passwordSetDate = (get-aduser $user -properties passwordlastset | foreach { $_.PasswordLastSet }) $PasswordPol = (Get-AduserResultantPasswordPolicy $user) # Check for Fine Grained Password if (($PasswordPol) -ne $null) { $maxPasswordAge = ($PasswordPol).MaxPasswordAge } else { $maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge } $expireson = $passwordsetdate + $maxPasswordAge $today = (get-date) $daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days $subject=”Your password will expire in $daystoExpire days” $body =” Dear $name,

Your password will expire in $daystoexpire day(s).
To change your password, do these things
For remote password changes, sign in to this address and change it there’

Thanks,
IT

” if (($daystoexpire -eq $expireindays1) -or ($daystoexpire -eq $expireindays2)) { Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High } }