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.

4.6 Star
80,343 times
Add to favorites
Active Directory
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Method invocation failed because [System.Int32] does not contain a method named 'split'.
    5 Posts | Last post April 11, 2019
    • Hi there,
      I have implemented this script on several servers. On 1 server I see this error in the logfile and the e-mail is not being send.
      What is missing or wrong overhere?
    • Any more info? Line number of the error, full error text? OS of the server?
    • I found the reason. The split is this line:
         if(($interval.split(",")) -Contains($daysToExpire.toString()))
      That isn't part of the script but on some servers the orginal line:
         if(($interval) -Contains($daysToExpire))
      Won't work because when using more days in the interval parameter you need the spplit. I thing its different per OS.
      That solution was posted earlier.
    • And when not using the split, it will not e-mail. It will keep saying in the log in the collum SendMail just an OK, nothing more. And the interval has got 5.
    • I think that depends how you pass the interval array into the script.
      What is the full command you use to run the script?
  • SMTP Issue while Testing
    4 Posts | Last post April 09, 2019
    • Hi there,
      Having issues getting the e-mail portion to work with g-mail. We will most likely use Office 365 smtp servers but I just wanted to get this working before I show it to my team.
      SMTP Server          :
      Expire in Days       : 21
      From                 :
      Logging              : True
      Log Path             : c:\logFiles
      Testing              : True
      Test Recipient       :
      Report Recipient     : 
      Intervals            : 
      Getting this error:
      The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. g1sm7204905ywf.0 - gsmtp
      I know my credentials are right, and I've followed your YT tutorial on creating the smtp.txt file as well as making it a securestring.
      Any thoughts?
      I've also added the PORT and USESSL portions to test and I'm not getting in my e-mail. Even without the port type, the error above is asking for some STARTTLS command.
      Send-Mailmessage -smtpServer $smtpServer -from $from -to $reportTo -subject $reportSubject -body $reportbody -bodyasHTML -priority High -Encoding $textEncoding -Attachments $logFile -Credential $credential Port 587 -UseSsl -ErrorAction Stop 
    • Have a look at the question on December 2nd 2018, that has the solution you need.
    • I only see questions from Dec 11 then it jumps to Nov 29. I'm missing something here. Please help.
    • Sorry don't know what happened, it was December 18th.
       [System.Net.ServicePointManager]::SecurityProtocol = 'TLS12' 
      You need to set the above prior to the sending the message, so i have added it to 'system settings'..
      # System Settings
      $textEncoding = [System.Text.Encoding]::UTF8
      $today = $start
      # Set TLS Version, used with SMTP Authentication
      [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
      # End System Settings
  • 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.
41 - 50 of 534 Items