Creating Transport Rules in Exchange 2007 using the EMS is very different from any other Exchange cmdlet. Because of the complex nature of conditions, exceptions and actions that can be used, we must use variables and arrays to create these rules.
Let’s have a look at how to create a basic rule to redirect every e-mail sent from Nuno’s mailbox that has a particular subject to the Quarantine mailbox.
If we want to have multiple Conditions, we need to assign each one to a different variable:
# For every e-mail sent by a specific user (in this case me)
$condition1 = Get-TransportRulePredicate From
$condition1.Addresses = @(Get-Mailbox Nuno)
# only when the e-mail is going Outside the organization
$condition2 = Get-TransportRulePredicate SentToScope
$condition2.Scope = @("NotInOrganization")
# and only when the subject contains any of these phrases
$condition3 = Get-TransportRulePredicate SubjectContains
$condition3.Words = @("FW: There was an error sending your mail", "FW: Mail delivery failed", "FW: failure notice")
Now we need to assign an Action:
# Redirect the e-mail to the Quarantine mailbox
$action = Get-TransportRuleAction RedirectMessage
$action.Addresses = @(Get-Mailbox Quarantine)
Finally, we create the TransportRule itself:
New-TransportRule -Name "Block Non Delivered e-mails from Nuno" -Comments "Prevent Nuno from forwarding NDRs e-mails" -Conditions @($condition1, $condition2, $condition3) -Actions @($action) -Enabled $True -Priority 0
If it’s this hard, why bother using the EMS to create Transport Rules? Well, in some cases, it might be useful for scripts or programs to automatically create a rule to block e-mails if they detect a Spam outage for example!
For more information: