Skip to content
Menu

Block Automatic External Forwarding for the Tenant

Sets the default outbound spam policy to block automatic external forwarding, with optional exempt senders. Needs DryRun=false and Approve=set-external-forwarding-block. Saves a before-state first.

Block automatic external forwarding for the tenant is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Block automatic external forwarding for the tenant. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, 11, Server 2016+, with the app certificate; runs as SYSTEM or a user
NeedsPowerShell 5.1; Graph: Organization.Read.All; ExchangeOnlineManagement 3.1+, Exchange.ManageAsApp, Exchange admin role
Changes thingsChanges what it runs on

What It Does

The script reads the tenant's default outbound spam filter policy and sets automatic forwarding to Off when needed. External inbox-rule and mailbox forwarding covered by this policy are blocked, with a non-delivery report. Internal forwarding is unaffected.

When you list ExceptionSenders, it creates or updates a separate outbound policy that allows forwarding and a rule with exactly the recipients you specify as senders. Leaving the list empty preserves any existing exception policy and rule. A disabled exception rule stays disabled and the script warns. It leaves other policies, mailbox forwarding settings and inbox rules alone.

It does nothing unless DryRun is false and Approve is set to set-external-forwarding-block. It saves the before-state to a file under the MSPOutpost Backups folder and prints the path. Afterwards it re-reads the policy several times, because Exchange can show a change late, and fails if the setting did not take.

On a brand-new tenant that is not yet customizable, organization customization is turned on first and the script stops there. Microsoft needs some time, often up to an hour, so run it again later. A second run after the change ends OK and says it is already blocked.

Preset security policies take precedence over custom ones, so a sender covered by a preset follows the preset's setting, not the exception.

Before You Run It

  • Run the forwarding monitor first. Check which listed senders fall under the default policy or a separate rule before you turn on the block.
  • Remove or approve the existing forwards with the client before you turn this on.
  • List the mailboxes that must keep forwarding, such as a scanner or a partner mailbox, in ExceptionSenders.
  • Run with DryRun=true and read the plan, then set DryRun=false and the Approve line.
  • Keep the before-state file the run prints. On a new tenant, expect to run it twice, an hour or so apart.
  • Give the app Exchange.ManageAsApp and a role that can change outbound spam policies, such as Exchange Administrator.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: High. It is a dry run unless you set both DryRun=false and Approve=set-external-forwarding-block, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
  • What it changes: Sets AutoForwardingMode Off on the default outbound spam filter policy; with ExceptionSenders also creates or updates one outbound spam filter policy and rule; writes a before-state JSON file.
  • Restart: It never restarts the machine.
  • Typical run: Under a minute.
  • Example answer: "Automatic external forwarding is now Off tenant-wide; 1 sender(s) exempt; before-state saved"
  • Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.

Inputs

Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.

  • TenantId: The client's tenant ID (a GUID) or domain such as contoso.onmicrosoft.com. Required.
  • ClientId: The app registration's Application (client) ID, a GUID. Required.
  • CertificateThumbprint: Thumbprint of the app's certificate, with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. Leave empty to use the ClientSecret environment variable.
  • ExceptionSenders: Mailbox addresses that may keep forwarding outside (for example a scanner or a shared mailbox that forwards to a partner), separated by comma, semicolon or new line. Default none: no exception policy is created or changed. When set, the list REPLACES the exception rule's senders.

Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.

License

Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.

The Script

set-external-forwarding-block.ps1 · 1148 lines · 65.7 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.