Automating email organization is one of the best ways to keep your inbox clean and efficient. As an admin or power user, being able to set up rules programmatically using PowerShell can save tons of time—especially across multiple users.
In this guide, we’ll walk you through the New-MgUserMailFolderMessageRule cmdlet and show how to create email rules directly in a user’s mailbox using Microsoft Graph PowerShell.
New-MgUserMailFolderMessageRule is a Microsoft Graph PowerShell cmdlet that allows you to create rules for specific mail folders in a user's mailbox. These rules automatically take actions on incoming messages based on defined conditions.
Think of it like Outlook rules—but set via script, perfect for bulk automation or remote configuration.
This cmdlet is incredibly helpful when you want to:
No need to rely on users manually setting up their rules—just automate it!
New-MgUserMailFolderMessageRule -UserId <String> -MailFolderId <String> -BodyParameter <Hashtable>
Parameters:
Automatically forward emails from any address that contains gmail.com:
$params = @{
displayName = "Forward Emails from Gmail"
sequence = 1
conditions = @{
senderContains = @("gmail.com")
}
actions = @{
forwardTo = @(
@{
emailAddress = @{
name = "Another User"
address = "anotheruser@domain.com"
}
}
)
stopProcessingRules = $true
}
}
New-MgUserMailFolderMessageRule -UserId "samadmin@7xh7fj.onmicrosoft.com" -MailFolderId "Inbox" -BodyParameter $params
This is ideal for redirecting certain emails to another recipient automatically.
Automatically mark emails containing the word “Sale” in the subject line as read:
$params = @{
displayName = "Mark Sale Emails as Read"
sequence = 2
conditions = @{
subjectContains = @("Sale")
}
actions = @{
markAsRead = $true
stopProcessingRules = $true
}
}
New-MgUserMailFolderMessageRule -UserId "samadmin@7xh7fj.onmicrosoft.com" -MailFolderId "Inbox" -BodyParameter $params
Great for decluttering promotional content or categorizing updates silently.
Yes. Just pass the MailFolderId of the folder you want. Use Get-MgUserMailFolder to find folder IDs.
Use the sequence property. Lower numbers run first.
Absolutely. Actions like markAsRead, moveToFolder, forwardTo, and more can be combined.
No. Rules only apply to new incoming messages from the moment they're created.
Redirect specific messages to another monitored mailbox.
Automatically mark or move newsletters, alerts, or sales emails.
Create priority-based rules to direct important emails to specific folders.
Forward or copy emails to archiving accounts or distribution lists for compliance.
The New-MgUserMailFolderMessageRule cmdlet makes it easy to set up smart inbox management rules across your Microsoft 365 environment. Whether you're automating workflows, decluttering inboxes, or enforcing organizational policies, this tool lets you create efficient, scalable mail rules with just a few lines of PowerShell.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex