Microsoft Outlook inbox rules allow users to automatically organize emails by applying predefined conditions. With Microsoft Graph PowerShell, administrators can create inbox rules programmatically, ensuring a more efficient email management system for users.
In this article, we will explore a Graph PowerShell script to create an inbox rule that moves emails from a specific sender to a designated folder in the mailbox.
The following script allows administrators to set up an inbox rule that automatically moves emails from a specified sender to a specific folder in the user’s mailbox.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.ReadWrite", "MailboxSettings.ReadWrite"
# Specify mailbox and rule parameters
$UserUPN = "user@yourtenant.onmicrosoft.com"
$SenderEmail = "important.sender@example.com"
$TargetFolderName = "Project Updates"
# Retrieve the folder ID
$TargetFolder = Get-MgUserMailFolder -UserId $UserUPN -Filter "displayName eq '$TargetFolderName'" -Select Id
$TargetFolderId = $TargetFolder.Id
# Define the rule
$InboxRule = @{
displayName = "Move Emails from $SenderEmail"
sequence = 1
conditions = @{ senderContains = @($SenderEmail) }
actions = @{ moveToFolder = $TargetFolderId }
isEnabled = $true
}
# Apply the rule
New-MgUserMailFolderMessageRule -UserId $UserUPN -MailFolderId "inbox" -BodyParameter $InboxRule
# Disconnect
Disconnect-MgGraph
Import-Csv "UserList.csv" | ForEach-Object {
$UserUPN = $_.UserPrincipalName
# Insert script logic here
}
conditions = @{
senderContains = @("important.sender@example.com")
subjectContains = @("Urgent")
importance = "high"
}
Get-MgUserMailFolderMessageRule -UserId $UserUPN -MailFolderId "inbox"
Remove-MgUserMailFolderMessageRule -UserId $UserUPN -MailFolderId "inbox" -MessageRuleId ""
| Error | Cause | Solution |
| Access Denied (403 Forbidden) | Missing permissions. | Ensure MailboxSettings.ReadWrite permission is granted in Azure AD and admin consent is provided. |
| Folder Not Found | The target folder name is incorrect or doesn’t exist in the mailbox. | Verify that the folder name is correct and exists in the user's mailbox. Run the following to list available folders: |
| Rule Already Exists | A similar rule already exists in the mailbox. | List existing rules before creating a new one: |
| API Throttling | Too many requests sent in a short time. | Add a short delay before executing requests. |
Using Graph PowerShell to create inbox rules automates email organization and enhances mailbox management. With this script, administrators can define custom email handling rules, reducing manual effort and improving workflow efficiency.
By extending the script to include bulk user processing, additional filters, and automated scheduling, IT teams can streamline inbox rule management for large organizations.
Give it a try and let us know how it helps!