AD Account Expiry Powershell

Anonymous
2023-12-11T20:44:17+00:00

hi All,

am looking for a powershell script which can send email to our helpdesk with all the list of users whose accounts are expiring in next 7 or 10 days. the list or table should need to have Name, SamAccount, Expiry Date, Manager.

Here is the script i have but getting error while executing the script.

Scirpt:

============================================================

Define the helpdesk email address

$helpdeskEmail = "******@mydomain.com"

Search for expiring accounts within the threshold

$expiringAccounts = = Search-ADAccount -AccountExpiring -UsersOnly -TimeSpan 10.00:00:00 | Get-ADUser -Properties Enabled, Manager, AccountExpirationDate, LastLogonDate | Select-Object Name,Manager,AccountExpirationDate,LastLogonDate,UserPrincipalName

Build the email body

$body = "The following Active Directory accounts will expire within the next 10 days:\n\n"

Use a table to format the account information

$body += "<table>
<thead>
<tr><th>Name</th><th>SAMAccountName</th><th>Expiration Date</th><th>UserPrincipalName</th><th>Manager</th></tr>
</thead>
<tbody>"

Loop through each expiring account and add a row to the table

foreach ($account in $expiringAccounts) {
# Get manager details
$manager = Get-ADUser -Identity ($account.Manager)

$body += "&lt;tr&gt;  

<td>$account.Name</td>
<td><span class="math-inline">account.SamAccountName</td&gt;
<td&gt;</span>($account.AccountExpirationDate).ToShortDateString()</td>
<td>$account.UserPrincipalName</td>
<td>$manager.Name</td>
</tr>"
}

$body += "</tbody></table>"

Send the email notification

Send-MailMessage -To $helpdeskEmail -From "******@mydomain.com" -Subject "Active Directory Accounts Expiring Soon" -Body $body -SmtpServer "smtp.office365.com"

========================================================================

Error:

PS C:&gt; C:\Users\ABCUser\Desktop\AccountExpiry.ps1
At C:\Users\ABCUser\Desktop\AccountExpiry.ps1:24 char:18

  • <td><span class="math-inline">account.SamAccountName</td&gt;
  •              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    

Unexpected token 'math-inline">account.SamAccountName</td&gt;
<td&gt;</span>($account.AccountExpirationDate).ToShortDateString()</td>
<td>$account.UserPrincipalName</td>
<td>$manager.Name</td>
</tr>"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

Windows for business | Windows Server | User experience | PowerShell

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question. To protect privacy, user profiles for migrated questions are anonymized.

0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-12-12T07:18:28+00:00

    Hi,

    Please replace the double quotes in <span class="math-inline"> with single quotes.

    0 comments No comments
  2. Anonymous
    2023-12-12T18:09:55+00:00

    hi Ian,

    thank you for the suggestion, however the script only gave the data of one user whos account is expired already and the employee is reporting to the account which was running the script.

    also the formatting is an issue

    this is how it looked

    The following Active Directory accounts will expire within the next 10 days:\n\n<table> <thead> <tr><th>Name</th><th>SAMAccountName</th><th>Expiration Date</th><th>UserPrincipalName</th><th>Manager</th></tr></thead>
    <tbody><tr>

    0 comments No comments
  3. Anonymous
    2023-12-13T08:53:47+00:00

    The script should get all the accounts expires in 10 days. Check the result of the command

    Search-ADAccount -AccountExpiring -UsersOnly -TimeSpan 10.00:00:00 
    

    and see if it gets only one AD account.

    Also note that you need to use “=” instead of "==" in the "$expiringAccounts = = Search-ADAccount" line.

    The output is exactly the same as what you added to $body. What do you expect the output to be like?

    0 comments No comments