Password Expiry Email Notification

This script will email a user in the event that their password is due to expire in X number of days. I have now moved it to GitHub -

4.6 Star
94,384 times
Add to favorites
Active Directory
E-mail Twitter Digg Facebook
  • Remove top line that is coming up in CSV report being emailed
    4 Posts | Last post April 07, 2019
    • Hello! Thank you so much for creating and maintaining/improving this script. It is used by me and I think it is one amazing script.
      One question - The is one line being added at the top of the CSV report which is emailed out by your script. The top of the report looks like this:
      #TYPE System.Object							
      UserMessage	UserName	Name	EmailAddress	PasswordSet	DaysToExpire	ExpiresOn	SendMail
      Is there a way to have the "#TYPE System.Object" removed the report? I located this info which may be of help to you:
      Thank you!
    • That looks quite convoluted to me.
      You can probably make this a bit prettier...
      $csvNew = "new-report.csv"
      $oldCSV = Get-Content "report.csv"
      $oldCSV = $oldCSV.trim("#TYPE System.Object")
      $oldCSV = $oldCSV | where { $_ -ne "" }
      New-Item $csvNew -ItemType file -Force
      Add-Content $csvNew $oldCSV
    • Can you tell me where in the latest version of your script I would need to place those lines? Thank you!
    • Right before the line where the report is sent.
  • Not sending email when task scheduler, or batch file used to run
    3 Posts | Last post March 26, 2019
    • I did see other comments that this issue has happened to others, I don't know If I saw an answer. I can run the script from a PowerShell console, it sends email to users with expiring accounts, but when using it with task scheduler, it logs the email as "skipped interval" I am calling from a batch file, using this syntax
      cd C:\Pscript
      powershell.exe -file C:\Pscript\PasswordChangeNotification.ps1 -smtpServer [FQDN of internal server name] -expireInDays 10 -from "Password Change <>" -Logging -LogPath "c:\SMTPFiles" -reportTo -interval 0,1,2,5,6,7,8,10
      any comments to what is my error would assist greatly, thank you in advance
    • The only time I saw "Skipped interval" was when my user was within the 10 days but was on day 4 which is not specified in the -interval command. The log file should show you this.  Also when you run from the TS that its running as the same user that was used through the other steps. Below is what is in my task scheduler, I had to remove the "" and replace them with '' inside the string while keeping the "" on the whole string.
      -command "C:\scripts\PasswordChangeNotification.ps1 -smtpServer smtp.mail.server.example -expireInDays 10 -from 'IT Support <>' -Logging -LogPath 'c:\logFiles' -interval 1,2,3,5,7,10"
    • I think that may have solved it, the sending address was a group, I have changed it to a user that can be added to the TS, I have run the script from explorer, it would fail this way prior. thank you
  • only 1 user Object Found
    2 Posts | Last post March 22, 2019
    • Ran script and it is only finding 1 user object. 
      I then tried running it as administrator and now it shows 28 user objects. However this domain has 100+ user accounts. It is also saying 0 users to notify when a manual report shows users will expire tomorrow. 
      Thank you for your assistance. 
    • Answering my own question.. Basically the script was working fine I had a misconception about the company 28 user objects were correct and so was the 0 notifications for my set time of 15 days. However I will share some new info I was able to change the smtp port for so this would work on an Azure server. Added in the Port parameter and moved the other parameters down.
          # $smtpServer Enter Your SMTP Server Hostname or IP Address 
          # $Port Enter Your SMTP Server port number 
          # Notify Users if Expiry Less than X Days 
          # From Address, eg "IT Support <>" 
      ect. res of the Param( section is the same just the Position numbers moved up one. 
      With this change the following command works. 
      Powershell.exe -ExecutionPolicy Bypass c:\PasswordExpire\PasswordChangeNotification.ps1 -smtpServer -Port 2500 -expireInDays 15 -from
  • Send Mail Failure
    2 Posts | Last post March 22, 2019
    • I apologize if this has been answered before, but I did not see it posted here.
      I am unable to get the send mail function to work correctly. I've made a few different modifications, but I continue to get this error even with the 'stock' script downloaded from here.
      "An invalid character was found in the mail header: '<'."
      I can't seem to find which '<' is causing the send mail piece to have issues. Have you seen this before?
    • Please disregard. I was simply missing the trailing '>' on the From address...
  • Get-ADDefaultDomainPasswordPolicy may not work for AWS provisioned domains
    1 Posts | Last post March 16, 2019
    • Hi!
      Just wanted to let you know that AWS doesnt' allow to modify default domain policy in their provisioned Windows AD. Instead they only give you permissions to administer only one OU so the Get-ADDefaultDomainPasswordPolicy always returns 42 days.
      Apparently they have something blocking the Get-AduserResultantPasswordPolicy too! despite the domain level is windows2012r2
      so I had to work around that and came to a solution like that:
      $defaultMaxPasswordAge = [convert]::ToInt32((net accounts | ForEach-Object { if ($_ -match "^Maximum password age \(days\):\s+(\d+)$"){$Matches.1}}),10)
      this always returns the correct policy set by the domain group policy applied to the OU server in.
      that also works great on the domain level windows2008
  • Can you recommend how to customize your script CC. manager of user
    2 Posts | Last post March 07, 2019
    • Hi Robert | I have configured your script and everything are working normally but my leader would like to customize your script must to CC. manager of user also. Could you recommend me or not ?
    • Yes someone else asked that in the comments below, you need to capture the users manager from AD then get their email address and add that to the -cc
  • How to Use This Script?
    2 Posts | Last post March 04, 2019
    • I have tried several time but not able to use this script properly do you have some video to understand?
      Yes, all the videos for version 2.
  • How to pass parameters
    3 Posts | Last post March 01, 2019
    • Hello 
      I will like to know how to pass parameters to script to send the report file to more then one person. I am using this -reportto;  but doesn't work.
    • Im fairly sure it is a comma separated list that Send-Mailmessage supports, not semi colon. However the parameter is expecting a string, so you may need to enclose your list inside quotes.
      -to ","
    • Hi Gary ,
      at the bototm pf the script
      $reportBody = "Password Expiry Report Attached"
                  Send-Mailmessage -smtpServer $smtpServer -from $from -to $reportTo.Split(",") -subject $reportSubject -body $reportbody -bodyasHTML -priority High -Encoding $textEncoding -Attachments $logFile -ErrorAction Stop 
                  $errorMessage = $_.Exception.Message
                  Write-Output $errorMessage
      change $reportTo  to  $reportTo.Split(",")   and add recipients by comma separated values on your task scheduler arguments  like this : 
      "C:\scripts\PasswordChangeNotification.ps1 -smtpServer <your.smtp.server> -expireInDays 11 -from '' -Logging -LogPath 'c:\logFiles'  -reportTo ',,' -interval 1,2,5,10"
      had the same issue and it is now solved. Robert if possible ammend that for your next version.
  • Task scheduler Access Denied
    6 Posts | Last post February 28, 2019
    • Hi I've been able to get the script running explicitly running from powershell itself and have followed the youtube guide for this.
      However when I try to execute my script with my custom AD user (everything is hosted on the domain controller) it gives me access denied.
      Task Scheduler failed to start instance "{instance id}" of "\Password expiry email"  task for user "domain\passwordreminderuser" . Additional Data: Error Value: 2147942405.
      Task Scheduler failed to launch action "C:\Windows\System32\WindowsPowerShell\v1.0\" in instance "{instance id}" of task "\Password expiry email". Additional Data: Error Value: 2147942405.
      For my user it didn't matter if I have it set to logon as batch, logon as local service and/or allow logon locally. In addition I added it to domain\administrators Group thinking that might be preventing it from executing Powershell however it does not work.
      1) Under general: Run whether user is logged on or not and with Highest priveledges
      2) General is set to run with my created passwordreminderuser already with correct password entered.
      3) I set the task settings to stop the existing instance of the task if its already running
      See imgur album of additional details including script i'm trying to execute
      I've tried numerous things such as using my domain admin to execute the script and it keeps failing with the same error, running it as logged on user. Only thing I haven't done yet is reboot the server. 
      And logs of course do not save when using task schedule.
    • Why is there an & at the begining of your command?
      Program/Script should just be 'powershell.exe'
      Please review,
    • Hi thanks for the update and for continually working with users who implement your script.
      I've reverted my modifications and now have it matching your syntax. However when I go to execute it, the task completes successfully with the last run result being 0x1
    • Does the script execute?
    • Hi yes it works its way through the task scheduler process however when it executes it gives:
      Task Scheduler successfully completed task "\Password expirey email" , instance "{54455454-e18d-4a7c-82da-49060c7d08cf}" , action "powershell.exe" with return code 1.
    • I think I resolved it, looks like it might've been failing to execute due to execution policy settings.
      As per:
      -noninteractive -nologo  -Command "&'D:\IT\Scripts\PasswordChangeNotification.ps1' -smtpServer -expireInDays 21 -from 'IT Support <>' -reportTo -interval 1,2,5,10,15 -testing -testrecipient -logging -logPath 'D:\IT\Scripts\Log Files'"
  • Mail not working via Task Scheduler
    2 Posts | Last post February 22, 2019
    • The script works fine. When running from a command via Powershell. But the same via the Task Scheduler won't fire the e-mail. And it does domething since the log file is updated. It's just the mail that is not being send as soon as I start it up via the Scheduler.
      Should I use -Command or -File ?
      -File "C:\admin\PasswordChangeNotification.ps1" -expireInDays 21 -Logging -testing -testRecipient -interval 1,3,7,8
      The other parameters are in the PS1. But the default also works the same way. Works via PowerShell, also Scheduler, except the mail.
    • I use -Command.
51 - 60 of 542 Items