Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Import Exchange Online module Import-Module Microsoft.Exchange.OnlineManagement # Create an array of keywords to match in email subjects $keywords = @("Spam", "Bulk Email", "Promotion") # Connect to Exchange Online Connect-ExchangeOnline # Get the email address of the mailbox to create rules for $emailAddress = Read-Host "Enter the email address of the mailbox: " # Get the current rules for the mailbox $currentRules = Get-InboxRule -Mailbox $emailAddress # Create the array of new rules $newRules = @() # Loop through the list of keywords foreach ($keyword in $keywords) { # Create a new inbox rule $newRule = New-InboxRule -Name "AutoRoute-$keyword" -Mailbox $emailAddress -StopProcessingRules $true -Enabled $true # Set the conditions for the rule $newRule.Conditions.SubjectContainsWords = $keyword # Set the actions for the rule $newRule.Actions.MoveToFolder = "Deleted Items" # Add the new rule to the array $newRules += $newRule } # Update the inbox rules for the mailbox Update-InboxRule -Mailbox $emailAddress -Rules $currentRules + $newRules # Confirm that the rules were created successfully Write-Host "Inbox rules have been updated to automatically route emails with the keywords 'Spam', 'Bulk Email', and 'Promotion' to the trash." |
Testing if the previous output contains the string New-PSSession
: False
Testing if the previous output contains the string Connect-ExchangeOnline
: True
Testing if the previous output contains the string -MoveToFolder
: False
Testing if the previous output contains the string -DeleteMessage
: False
Testing if the previous output contains the string -SubjectOrBodyContainsWords
: False
Testing if the previous output contains the string -SubjectContainsWords
: False
Testing if the previous output contains the string New-InboxRule
: True
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Import the Exchange Online PowerShell module Import-Module ExchangeOnlineManagement # Connect to Exchange Online Connect-ExchangeOnline # Create a new inbox rule to move emails to the trash if the subject contains "Spam" New-InboxRule -Name "MoveSpamToTrash" -MoveToFolder "Deleted Items" -SubjectContainsWords "Spam" # Create a new inbox rule to move emails to the trash if the subject contains "Junk" New-InboxRule -Name "MoveJunkToTrash" -MoveToFolder "Deleted Items" -SubjectContainsWords "Junk" # Enable the inbox rules Enable-InboxRule -Identity "MoveSpamToTrash" Enable-InboxRule -Identity "MoveJunkToTrash" |
Testing if the previous output contains the string New-PSSession
: False
Testing if the previous output contains the string Connect-ExchangeOnline
: True
Testing if the previous output contains the string -MoveToFolder
: True
Testing if the previous output contains the string -DeleteMessage
: False
Testing if the previous output contains the string -SubjectOrBodyContainsWords
: False
Testing if the previous output contains the string -SubjectContainsWords
: True
Testing if the previous output contains the string New-InboxRule
: True
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # Create mail rule to move emails with specific words in subject to Deleted Items folder function Create-MailRule { [CmdletBinding(DefaultParameterSetName = "Default")] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $DisplayName, [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string[]] $Words, [Parameter(Mandatory = $false)] [ValidateSet("Enabled", "Disabled")] [string] $State = "Enabled" ) New-InboxRule -DisplayName $DisplayName -Actions ([ValidateSet("MoveToFolder", "Delete")]::Delete) -MoveToFolder "\Deleted Items" -SubjectContainsAnyWords $Words -State $State } # Enable all disabled mail rules with specific words in subject function Enable-MailRules { [CmdletBinding(DefaultParameterSetName = "Default")] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string[]] $Words ) Get-InboxRule -SubjectContainsAnyWords $Words -State Disabled | ForEach-Object { Set-InboxRule $_ -State Enabled } } # Delete all mail rules with specific words in subject function Remove-MailRules { [CmdletBinding(DefaultParameterSetName = "Default")] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string[]] $Words ) Get-InboxRule -SubjectContainsAnyWords $Words | ForEach-Object { Remove-InboxRule $_ } } $words = @("spam", "junk", "phishing", "advertisement") $displayName = "Spam and Phishing Filter" Create-MailRule -DisplayName $displayName -Words $words Enable-MailRules -Words $words |
Testing if the previous output contains the string New-PSSession
: False
Testing if the previous output contains the string Connect-ExchangeOnline
: False
Testing if the previous output contains the string -MoveToFolder
: True
Testing if the previous output contains the string -DeleteMessage
: False
Testing if the previous output contains the string -SubjectOrBodyContainsWords
: False
Testing if the previous output contains the string -SubjectContainsWords
: False
Testing if the previous output contains the string New-InboxRule
: True
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
1 2 3 4 5 6 7 8 9 | $keywords = "Delete", "Junk", "Spam" # Get the currently logged in user $user = Get-User # Create a new inbox rule for each keyword foreach ($keyword in $keywords) { New-InboxRule -Name "Delete messages with subject containing '$keyword'" -InboxRule -Mailbox $user.alias -Conditions @{SubjectContainsWords = $keyword} -Actions @{MoveToFolder = "Deleted Items"} -Enabled $true } |
Testing if the previous output contains the string New-PSSession
: False
Testing if the previous output contains the string Connect-ExchangeOnline
: False
Testing if the previous output contains the string -MoveToFolder
: False
Testing if the previous output contains the string -DeleteMessage
: False
Testing if the previous output contains the string -SubjectOrBodyContainsWords
: False
Testing if the previous output contains the string -SubjectContainsWords
: False
Testing if the previous output contains the string New-InboxRule
: True
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
Create a Powershell script that creates and enables email inbox rules that automatically route incoming emails to the trash if the subjects contain certain keywords
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Add-Type -AssemblyName Microsoft.Office.Interop.Outlook $olApp = New-Object -comobject Outlook.Application $olNS = $olApp.GetNamespace("MAPI") $folder = $olNS.GetDefaultFolder(6) # Inbox $keywords = "keyword1", "keyword2", "keyword3" foreach ($keyword in $keywords) { $filter = "`Subject:\" + $keyword + "`" $rule = $folder.CreateRule($filter, "Delete " + $keyword) $rule.ExecuteImmediately = $true $rule.Enabled = $true } $olApp.Quit() |
Testing if the previous output contains the string New-PSSession
: False
Testing if the previous output contains the string Connect-ExchangeOnline
: False
Testing if the previous output contains the string -MoveToFolder
: False
Testing if the previous output contains the string -DeleteMessage
: False
Testing if the previous output contains the string -SubjectOrBodyContainsWords
: False
Testing if the previous output contains the string -SubjectContainsWords
: False
Testing if the previous output contains the string New-InboxRule
: False