Automate Outlook Inbox Rules with Graph PowerShell

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 Script: Create an Inbox Rule Using Graph PowerShell

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
                            

How the Script Works

  1. Connect to Microsoft Graph
    • The script authenticates to Microsoft Graph using Connect-MgGraph with the required permissions (Mail.ReadWrite and MailboxSettings.ReadWrite).
  2. Retrieve the Target Folder ID
    • The script searches for the specified folder (e.g., "Project Updates") in the user's mailbox and retrieves its folder ID.
  3. Define the Inbox Rule
    • The rule specifies that all emails from a given sender should be moved to the designated folder.
  4. Apply the Rule to the Inbox
    • The New-MgUserMailFolderMessageRule cmdlet applies the rule to the Inbox folder.
  5. Display Confirmation and Handle Errors
    • If successful, the script confirms that the rule has been applied. Otherwise, it displays the error message.
  6. Disconnect from Microsoft Graph
    • The script ends the session to free up resources.

Further Enhancements

  1. Apply Inbox Rules to Multiple Users
    • Instead of applying the rule to just one user, modify the script to apply it to multiple mailboxes.
    • Import-Csv "UserList.csv" | ForEach-Object {
          $UserUPN = $_.UserPrincipalName
          # Insert script logic here
      }
      
  2. Create Rules with Multiple Conditions
    • Modify the script to include subject filtering, importance level, or recipient checks.
    • conditions = @{
          senderContains = @("important.sender@example.com")
          subjectContains = @("Urgent")
          importance = "high"
      }
                                      
  3. List and Export Existing Inbox Rules
    • Before creating a new rule, administrators might want to list existing inbox rules
    • Get-MgUserMailFolderMessageRule -UserId $UserUPN -MailFolderId "inbox"
  4. Delete an Existing Inbox Rule
    • If an existing rule conflicts with the new one, it can be removed before applying the updated rule.
    • Remove-MgUserMailFolderMessageRule -UserId $UserUPN -MailFolderId "inbox" -MessageRuleId ""
  5. Schedule Automatic Rule Application
    • Automate rule creation using Task Scheduler or Azure Automation.

Possible Errors and Solutions

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:
Get-MgUserMailFolder -UserId $UserUPN | Select-Object DisplayName, Id
Rule Already Exists A similar rule already exists in the mailbox. List existing rules before creating a new one:
Get-MgUserMailFolderMessageRule -UserId $UserUPN -MailFolderId "inbox"
API Throttling Too many requests sent in a short time. Add a short delay before executing requests.
Start-Sleep -Seconds 2

Conclusion

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!