Send Automated Emails to Users with Graph PowerShell

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.

The Script: Send Automated Email to Multiple Users

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

How the Script Works

  • Connect-MgGraph: The script begins by connecting to Microsoft Graph using the Mail.Send permission, which allows the script to send emails on behalf of the user.
  • Define the Recipients and Email Details: The script defines the list of recipients who will receive the email and sets up the email’s subject and body content. This section can be customized based on your message and target audience.
  • Create and Send Email Draft: The 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.
  • Loop through Recipients: The script loops through the list of recipients, sending the email to each one individually and providing a confirmation message in the console.
  • Disconnect-MgGraph: Finally, the script disconnects from Microsoft Graph to end the session.

Further Enhancements

  • Load Recipients from CSV: Modify the script to load a list of recipients from a CSV file to make it more scalable when dealing with a large number of users.
  • $recipients = Import-Csv "C:\RecipientsList.csv" | Select-Object -ExpandProperty EmailAddress
  • Personalize the Email Content: You can include personalized information for each recipient in the email body to make the communication more engaging and relevant.
  • $bodyContent = "Dear $($recipient.DisplayName),

    We have an important update for you..."
  • Add Attachments to the Email: Enhance the script to include attachments by specifying the file path in the message body. This is useful when distributing important documents or notices.
  • Schedule Automated Emails: Use Task Scheduler or Azure Automation to run the script at specific intervals, ensuring that notifications or reminders are sent out automatically.

Possible Errors & Solutions

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.
You Can Only Send Emails from Accounts with Active Mailboxes

The Send-MgUserMessage cmdlet requires the sender to have a licensed Exchange Online mailbox.

Sending emails from guest accounts or unlicensed users without a mailbox will result in permission or mailbox-related errors.
Use HTML in the Message Body for Structured, Branded Emails

When composing automated messages, you can format the body using HTML.

This allows you to include branding elements, hyperlinks, styling, and layout — ideal for polished announcements and bulk notifications.

Conclusion

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