Script Repository

Create transport rule to block mail to Exchange recipient

February 23, 2021

The script creates an Exchange transport rule that blocks incoming mail to a recipient. You can use it, for example, to block mail to disabled users.

To use the script with Adaxes, you can add it to a business rule, custom command or scheduled task using the Run a program or PowerShell script action.


  • $exchangeServer - Specifies the fully qualified domain name (FQDN) of your Exchange Server.
  • $ruleName - Specifies a name for the new transport rule.
  • $rejectMessageEnhancedStatusCode - Specifies a status code that will be returned by Exchange when rejecting messages to the recipient.
  • $rejectMessageReasonText - Specifies a reason that explains why the message was rejected.
You can use value references (e.g. %name%) in the rule name and reject message text. When the script is executed, the value references are replaced with property values of the recipient. For example, if you specify the following name for the transport rule: Block mail to %name%, and execute the script on a user mailbox whose name is John Doe, the resulting rule name will be Block mail to John Doe.
Edit Remove
$exchangeServer = "" # TODO: modify me
$ruleName = "Block mail to %name%" # TODO: modify me
$rejectMessageEnhancedStatusCode = "5.7.929" # TODO: modify me
$rejectMessageReasonText = "Regretfully the user that you attempted to contact is no longer with Contoso Corporation" # TODO: modify me

    $session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
    Import-PSSession -session $session -DisableNameChecking -AllowClobber
    New-TransportRule $ruleName `
        -SentTo "%distinguishedName%" `
        -RejectMessageEnhancedStatusCode $rejectMessageEnhancedStatusCode `
        -RejectMessageReasonText $rejectMessageReasonText `
        -Enabled $True
    Remove-PSSession -Session $session

Comments ( 0 )
No results found.
Leave a comment