# Inspired by Martin Pugh as posted here: # http://community.spiceworks.com/topic/297650-warning-that-ad-account-will-expire # Be sure to adjust the next 4 items between "" for your needs and check all other CAPITAL wording for replacements... Param ( [string]$TimeSpan = "3", [string]$To = "YOURSELF@YOURCOMPANY.com", [string]$From = "YOURServiceDesk@YOURCOMPANY.com", [string]$SMTPServer = "YourSMTPRelayServer" ) Try { Import-Module ActiveDirectory -ErrorAction Stop } Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?"; Break } # Narrowing the search down to one specific OU while ignoring one sub OU $ExpiringUsers = Search-ADAccount -AccountExpiring -TimeSpan $TimeSpan -SearchBase "OU=USERSOU,DC=YOURCOMPANY,DC=COM" | Where-Object { $_.DistinguishedName -notlike "*OU=EXCLUDEDSUBOU,OU=USERSOU,DC=YOURCOMPANY,DC=COM*"} # First, email administrator a list on who's expiring: If ($ExpiringUsers) { $Header = @" "@ $Pre = "Accounts expiring in $TimeSpan Days" $Body = $ExpiringUsers | Select SamAccountName,Name,AccountExpirationDate | ConvertTo-HTML -PreContent $Pre -Head $Header | Out-String $SMTPSettings = @{ To = $To From = $From Subject = $Pre SMTPServer = $SMTPServer } Send-MailMessage @SMTPSettings -Body $Body -BodyAsHtml } #Now email each user ForEach ($User in $ExpiringUsers) { $ADUser = Get-ADUser $User.SamAccountName -Properties EmailAddress $Body = @" Dear $($ADUser.GivenName), Your YOURCOMPANY account $($ADUser.SamAccountName) will expire on $($User.AccountExpirationDate). Please contact your local Human Resources Manager as soon as possible for an account extension. Not responding within the given time frame results in a suspension of your account and access to your YOURCOMPANY resources will be blocked. Examples being: - Logging into PC/Laptop. - Access to remote services (YOURCOMPANY Desktop/VPN). - Disruption of email flow on mobile devices. Contact for you - Your local HR manager Process - Our systems detect an account in the User Accounts Country Organisational Unit will expire in $TimeSpan days - An automatic message is sent to the employee in question - Employee receives email and contacts HR Manager for an account extension - HR Manager sends and email to the Service Desk, confirming the account extension - Contains Employee name and new date of extension. - Service Desk informs Account Management and account is extended Your Service Desk +1 234 5678910 Option 1 - YOURServiceDesk@YOURCOMPANY.com - HQ Office "@ $SMTPSettings = @{ To = $ADUser.EmailAddress From = $From Subject = "Your account is expiring soon!" SMTPServer = $SMTPServer } # Method 1 to send to the targeted expiring end users, if used be sure to comment (hash) the next line out and remove the hash from your second choice option: Send-MailMessage @SMTPSettings -Body $Body # Or through this way Method 2: # Send-Mailmessage -smtpServer $SMTPServer -from $From -to $ADUser.EmailAddress -subject "You account is expiring soon!" -body $Body -priority High # Or for testing purposes to yourself only instead of all the end users # Send-Mailmessage -smtpServer $SMTPServer -from $From -to YOURSELF@YOURCOMPANY.com -subject "You account is expiring soon!" -body $Body -priority High }