This script uses the Get-ADUser cmdlet to retrieve user accounts that are enabled, have a set expiration date, and are expiring within the next 7 days. It then loops through the accounts and adds their name, email address, and expiration date to an HTML table in the email body. Finally, the script sends the email using the Send-MailMessage cmdlet, specifying the SMTP server, sender address, recipient address, subject, HTML body, and the -BodyAsHtml parameter to indicate that the email body is in HTML format.

Note that you’ll need to replace the $smtpServer, $fromAddress, and $toAddress variables with your own values. You may also need to modify the Get-ADUser filter to match your organization’s requirements.

# Set variables
$smtpServer = "your-smtp-server.com"
$fromAddress = "sender@yourdomain.com"
$toAddress = "recipient@yourdomain.com"
$subject = "Account Expiry Report"
$body = ""

# Set the report end date to 7 days from now
$startdate=(get-date)
$reportEndDate = (Get-Date).AddDays(7)

# Get all user accounts whose expiration date is within the next 7 days
$expiringAccounts = Get-ADUser -Filter {Enabled -eq $true -and AccountExpirationDate -gt $startdate -and AccountExpirationDate -lt $reportEndDate} -Properties AccountExpirationDate, EmailAddress

# Generate the email body

$emailbody += "<html><body><table border='1' cellpadding='5'><tr><th>Name</th><th>Email</th><th>Expiration Date</th></tr>"
foreach ($account in $expiringAccounts) {
    $emailBody += "<tr><td>$($account.Name)</td><td>$($account.EmailAddress)</td><td>$($account.AccountExpirationDate)</td></tr>"
}
$emailBody += "</table></body></html>"



# Send HTML email
$credentials = new-object Management.Automation.PSCredential $smtpUsername, ($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)  

Send-MailMessage -SmtpServer $smtpserver -Port 587 -UseSsl -Credential $credentials -To amitceh@gmail.com -From $from -Subject $subject -Body $emailbody -BodyAsHtml


By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *