Email Rules via PowerShell
Email inbox rules can be accessed for each user via the MS Office Outlook 2016 or in Outlook Webmail.
Administrators can access users’ inbox rules via PowerShell when logged into Exchange Online.
The instructions below explain how to connect and how to managed email rules in users’ inbox. They also show examples on how to set and remove rules, and how to loop through mailboxes for a group of users that meet certain criteria.
Refer to SOP “Connect to Office 365 with PowerShell” for more details.
- Open up PowerShell as Administrator.
-
Connect to Exchange Online and log in using a BRP O365 administrator account (you will be prompted for login):
Connect-ExchangeOnline
-
Examples of important commands in ExchangeOnline are; Get-InboxRule, Remove-InboxRule, New-InboxRule
For details and other helpful commands, refer to: https://learn.microsoft.com/en-us/powershell/module/exchange/?view=exchange-ps.
Examples:
Get all inbox rules in a mailbox:
Get-InboxRule -Mailbox oddarne
Output:
Name Enabled Priority RuleIdentity
---- ------- -------- ------------
TestForward True 1 17025766603611963393
Office Alert True 2 17097824197649891329
FAB Report True 3 7866993202744524801
SMTP Relay True 4 7939050796782452737
Wyatt Pult True 5 8011108390820380673
TRANSMAINT True 6 8083165984858308609
RDNHMI True 7 8155223578896236545
FNB True 8 8227281172934164481
Get details for a specific inbox rule in a mailbox:
Get-InboxRule -Mailbox oddarne -Identity "TestForward" |FL
Remove a specific inbox rule in a mailbox:
Remove-InboxRule -Mailbox oddarne -Identity "TestForward"
Use flag -Confirm:$false to omit verification.
Create new inbox rule in mailbox:
We want a rule that automatically forwards an email as an attachment to emailalerts@bedfordreinforced.com if:
- the email body contains “You don't often get email from” or “Some people who received this message don't often get email from”
- the sender email address does not contain “@bedfordreinforced.com”, “@bedfordplastics.com”, “@reinforcedlogistics.com”, “@oscosafety.com” or “@oilfieldsupplycompany.com”
- the subject doesn’t contain: “Re:” or “FW:”
New-InboxRule -Mailbox oddarne –Name "BRP Admin Rule" -BodyContainsWords "often get email from" -ForwardAsAttachmentTo emailalerts@bedfordreinforced.com -ExceptIfSubjectContainsWords "Re:","FW:" –ExceptIfFromAddressContainsWords "@bedfordreinforced.com", "@bedfordplastics.com","@oscosafety.com","@oilfieldsupplycompany.com","@reinforcedlogistics.com"
List all desired mailboxes:
Get-Mailbox -ResultSize unlimited | Where-Object {$_.alias -notlike "RLCompanyVeh*"} | Where-Object {$_.alias -notlike "StrategicPla*"} | Where-Object {$_.alias -notlike "DiscoverySea*"} | Where-Object {$_.alias -notin ("2015Chrysler","2012Impala","2016Chrysler","Allen.Taxes","BrianS","BrianTaxes","BRPefax","CorporateConfRm","CorpTrainingRm","emailalerts","frpsales","RanchHouse","River.House","TrainingRoom")}
Loop through all desired mailboxes, and remove any existing Admin rules, create new admin rules:
Make sure to change the variable sFile before running this script
$mailboxes = Get-Mailbox -ResultSize unlimited | Where-Object {$_.alias -notlike "RLCompanyVeh*"} | Where-Object {$_.alias -notlike "StrategicPla*"} | Where-Object {$_.alias -notlike "DiscoverySea*"} | Where-Object {$_.alias -notin ("2015Chrysler","2012Impala","2016Chrysler","Allen.Taxes","BrianS","BrianTaxes","BRPefax","CorporateConfRm","CorpTrainingRm","emailalerts","frpsales","RanchHouse","River.House","TrainingRoom")}
$sFile = "c:\users\oddarne\desktop\test.txt"
Set-Content -Path $sFile ""
$qtymailboxes = 0
$qtyrulesfound = 0
$qtyadminrulesfound = 0
$qtyadminrulescreated = 0
foreach ($mailbox in $mailboxes) {
$qtyorigrulesthismailbox = 0
$qtynewrulesthismailbox = 0
$qtymailboxes++
$inboxrules = Get-InboxRule -Mailbox $mailbox.alias
Write-Output "$($mailbox.alias):"
Add-Content -Path $sFile "$($mailbox.alias):"
foreach ($inboxrule in $inboxrules) {
$qtyorigrulesthismailbox++
$qtyrulesfound++
if ($inboxrule.Name.Equals("BRP Admin Rule")) {
Remove-InboxRule -Identity $inboxrule.Identity -Confirm:$false
$qtyadminrulesfound++
Write-Output " $($inboxrule.Name) ($($inboxrule.Identity)) - DELETED"
Add-Content -Path $sFile " $($inboxrule.Name) ($($inboxrule.Identity)) - DELETED"
}
else {
Write-Output " $($inboxrule.Name) ($($inboxrule.Identity))"
Add-Content -Path $sFile " $($inboxrule.Name) ($($inboxrule.Identity))"
}
}
New-InboxRule -Mailbox $mailbox -Name "BRP Admin Rule" -BodyContainsWords "often get email from" -ForwardAsAttachmentTo emailalerts@bedfordreinforced.com -ExceptIfSubjectContainsWords "Re:","FW:" -ExceptIfFromAddressContainsWords "@bedfordreinforced.com", "@bedfordplastics.com","@oscosafety.com","@oilfieldsupplycompany.com","@reinforcedlogistics.com"
$qtyadminrulescreated++
$inboxrules = Get-InboxRule -Mailbox $mailbox.alias
foreach ($inboxrule in $inboxrules) {
$qtynewrulesthismailbox++
if ($inboxrule.Name.Equals("BRP Admin Rule")) {
Write-Output " $($inboxrule.Name) ($($inboxrule.Identity)) - CREATED"
Add-Content -Path $sFile " $($inboxrule.Name) ($($inboxrule.Identity)) - CREATED"
}
}
Write-Output " Original rules in this mailbox: $($qtyorigrulesthismailbox)"
Add-Content -Path $sFile " Original rules in this mailbox: $($qtyorigrulesthismailbox)"
Write-Output " Current rules in this mailbox: $($qtynewrulesthismailbox)"
Add-Content -Path $sFile " Current rules in this mailbox: $($qtynewrulesthismailbox)"
Write-Output ""
Add-Content -Path $sFile ""
}
Write-Output "Total mailboxes: $($qtymailboxes)"
Add-Content -Path $sFile "Total mailboxes: $($qtymailboxes)"
Write-Output "Total rules found: $($qtyrulesfound)"
Add-Content -Path $sFile "Total rules found: $($qtyrulesfound)"
Write-Output "Total admin rules found and deleted: $($qtyadminrulesfound)"
Add-Content -Path $sFile "Total admin rules found and deleted: $($qtyadminrulesfound)"
Write-Output "Total admin rules created: $($qtyadminrulescreated)"
Add-Content -Path $sFile "Total admin rules created: $($qtyadminrulescreated)"