Jump to content

Powershell Script to notify users of expiring passwords


Recommended Posts

Και ένα μικρό Script για να ειδοποιούνται οι χρήστες πως οι κωδικοί τους θα λήξουν σε X μέρες :)

Script to notify users of password change
#What date is it?
#How many days before should we notify the user?
#Specify a From address
$From="[email protected]"
#Specify a subject for the message!
$Subject="Your account will expire soon! Action Required"
#Specify the SMTP Server !

#Find all users that will need a password change within the specified days, We are searching for all Enabled accounts , with Password never expires option not set and not containing a $ sign (Domains with trust accounts,GMSA accounts ...etc, we do not need those)
$Expiring=Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and Name -notlike "*$*"} –Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" ,"PasswordExpired" , "Mail","UserPrinciPalName"
#Loop through each account
foreach ($Account in $Expiring)
    #And get its expiring date, convert it to a more readable format as  msDS-UserPasswordExpiryTimeComputed is a calculated field and it is a mess
        #If you do find an account that has not expired and the remaining days are less than the days specified
        if ($Account.PasswordExpired -eq $false -and ($ExpiringDate - $Today).Days -lt $Days )
            #Start the notification process
            #Get the actual UPN in the domain as this may differ from the users email
            #Calculate the days left
            #Inform the user
            Send-MailMessage -SmtpServer $SMTPServer -From $From -To $Account.Mail -Subject $Subject  -Body "Your AD Password for account  $AccountUPN will expire in $DaysLeft days! Please change it."  
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...