Add mail rule with keywords in exchnage online with command
To add a mail rule with keywords in Exchange Online using the Exchange Online PowerShell module, you can use the following command:
New-MailRule -Name <RuleName> -Conditions @{ContainsKeywords = @("keyword1", "keyword2",...)} -Actions @{MoveToFolder = "<FolderName>"}
Here:
<RuleName>
is the name you want to give to the rule.@{ContainsKeywords = @("keyword1", "keyword2",...)}
specifies the conditions for the rule. In this case, the rule will apply to messages that contain one or more of the specified keywords.@{MoveToFolder = "<FolderName>"}
specifies the action for the rule. In this case, the rule will move the messages that match the conditions to the specified folder.
For example, to create a rule that moves messages that contain the keywords "urgent" or "important" to a folder named "Urgent", you would use the following command:
New-MailRule -Name "Urgent Messages" -Conditions @{ContainsKeywords = @("urgent", "important")} -Actions @{MoveToFolder = "Urgent"}
You can also use other conditions and actions, such as:
@{ContainsSubject = @("subject1", "subject2",...)}
to apply the rule to messages with specific subjects.@{ContainsFrom = @("from1", "from2",...)}
to apply the rule to messages from specific senders.@{ContainsTo = @("to1", "to2",...)}
to apply the rule to messages sent to specific recipients.@{MoveToFolder = "<FolderName>"}
to move messages to a specific folder.@{DeleteMessage = $true}
to delete messages that match the conditions.@{ForwardMessage = "<RecipientEmail>"}
to forward messages to a specific recipient.
You can also use the New-MailRule
cmdlet with the -Query
parameter to specify a more complex query using the Exchange Search syntax. For example:
New-MailRule -Name "Urgent Messages" -Query "subject:urgent OR subject:important" -Actions @{MoveToFolder = "Urgent"}
This will create a rule that applies to messages with subjects that contain the words "urgent" or "important", and moves them to the "Urgent" folder.
Note: You need to have the Exchange Online PowerShell module installed and configured on your system to use these commands. You can install the module by running the following command: Install-Module -Name ExchangeOnline