Communicating important messages to users efficiently is a key responsibility for administrators managing Microsoft 365 environments. Whether it's sending system alerts, notifications, or general updates, automating this task can save significant time and ensure that all intended recipients receive the message without delay.
In this article, we’ll guide you through a PowerShell script that leverages Microsoft Graph to send automated emails to multiple users. This approach is ideal for IT administrators who need to broadcast messages quickly and seamlessly.
Here’s the PowerShell script to send automated emails to multiple recipients using Microsoft Graph:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.Send"
# Define the list of recipients (you can also use a CSV file to load multiple recipients)
$recipients = @("user1@yourdomain.com", "user2@yourdomain.com", "user3@yourdomain.com")
# Define the email details
$subject = "Important Notification: System Downtime"
$bodyContent = "Dear User,
Please be informed that there will be a scheduled system downtime on Saturday from 10:00 AM to 12:00 PM. Kindly save your work and log off before this time.
Thank you for your cooperation.
Best Regards,
Your IT Team"
$senderEmail = "itadmin@yourdomain.com"
# Loop through each recipient and send the email
foreach ($recipient in $recipients) {
$emailMessage = @{
Subject = $subject
Body = @{
ContentType = "HTML"
Content = $bodyContent
}
ToRecipients = @(@{ EmailAddress = @{ Address = $recipient } })
}
# Create the email message draft
$draftMessage = New-MgUserMessage -UserId $senderEmail -BodyParameter $emailMessage
# Send the draft message
Send-MgUserMessage -UserId $senderEmail -MessageId $draftMessage.Id
Write-Host "Email sent to: $recipient"
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Mail.Send
permission, which allows the script to send emails on behalf of the user.New-MgUserMessage
cmdlet creates the email as a draft first, and the Send-MgUserMessage
cmdlet sends the created draft email using its unique message ID. This two-step process ensures that the email is properly structured before it is sent.$recipients = Import-Csv "C:\RecipientsList.csv" | Select-Object -ExpandProperty EmailAddress
$bodyContent = "Dear $($recipient.DisplayName),
We have an important update for you..."
Error | Cause | Solution |
Missing an argument for parameter 'Sender' | The email message is not structured correctly for the New-MgUserMessage cmdlet. |
Ensure that the email details such as subject and body are correctly specified within the $emailMessage hashtable. |
Insufficient privileges to complete the operation. | The account running the script does not have the required permissions. | Make sure the account has the Mail.Send permission granted in Azure AD with admin consent if necessary. |
The term 'Send-MgUserMessage' is not recognized. | The Microsoft Graph PowerShell module might not be installed or updated. | Install or update the Microsoft Graph PowerShell module by running Install-Module Microsoft.Graph . |
Invalid recipient address. | One or more recipient email addresses might be incorrect or not properly formatted. | Double-check that the recipient addresses are in a valid email format. |
Send-MgUserMessage
cmdlet requires the sender to have a licensed Exchange Online mailbox.body
using HTML.Automating email communication with Microsoft Graph PowerShell is a powerful way to manage mass notifications within your Microsoft 365 environment. This script allows administrators to quickly send personalized emails to multiple users without manual intervention, saving time and ensuring consistent messaging.
With further enhancements like loading recipients from a CSV file, adding attachments, or scheduling regular notifications, you can turn this basic automation into a versatile tool that meets the communication needs of your organization. Using Microsoft Graph to automate these tasks not only increases efficiency but also improves overall productivity for IT administrators.
© m365corner.com. All Rights Reserved. Design by HTML Codex