Skip to content
Menu

Mail Forwarding to Outside Addresses - Turn It Off

Clears external mailbox forwarding and disables (or removes) inbox rules that forward outside, after saving a before-state file. Needs DryRun=false and Approve=remove-external-mail-forwarding.

Mail forwarding to outside addresses - turn it off 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 Mail forwarding to outside addresses - turn it off. 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 uses the same checks as the forwarding monitor. It lists mailbox forwarding to external or unresolved recipients and enabled inbox rules that forward or redirect outside the tenant's accepted domains and AllowedDomains. DryRun stops at the plan. Which inbox rules it can check depends on the visible rules that Exchange returns.

To change anything you must set DryRun=false and Approve=remove-external-mail-forwarding. Then it saves the before-state of every affected mailbox and rule to a file under the MSPOutpost Backups folder and prints the path. Keep that file: it is how you restore a forward that turns out to be legitimate.

It clears each flagged mailbox forwarding setting and disables the entire matching inbox rule by default. That also stops any internal targets on the same rule. RuleAction=Remove deletes the rule instead. If more than MaxChanges changes are planned, it stops with nothing changed, so check the dry run and raise MaxChanges on purpose.

Afterwards it repeats the forwarding check on every touched mailbox and fails if the check still finds an external forward. Read any warnings about inbox rules it could not inspect. Changing an inbox rule from Exchange PowerShell also removes that mailbox's Outlook client-side rules.

If the mailbox was compromised, clearing the forward is not enough: reset the password, revoke sessions and look for other persistence.

Before You Run It

  • Run the monitor and a dry run first, and read every forward in the list.
  • Check with the client that none of the flagged forwards is wanted. Review rules with mixed destinations, and add legitimate external domains to AllowedDomains.
  • Keep the before-state file the run prints. It is the way back.
  • Warn the user that their Outlook client-side rules in a touched mailbox are removed as well.
  • For a hijacked mailbox, also reset the password, revoke sessions and check for other persistence.
  • Give the app Exchange.ManageAsApp and a role that can change mailboxes and inbox rules, 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=remove-external-mail-forwarding, 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: Clears external ForwardingSmtpAddress and ForwardingAddress on mailboxes; disables or removes inbox rules that forward or redirect outside; writes a before-state JSON file.
  • Restart: It never restarts the machine.
  • Typical run: 1 to 10 minutes on a large tenant.
  • Example answer: "Cleared external forwarding on 2 mailbox(es), disabled 1 rule(s); 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.
  • AllowedDomains: Extra domains that are fine to forward to, separated by comma, semicolon or new line. Default none.
  • TargetMailbox: Work on one mailbox only (an address, alias or UPN). Default empty: every user and shared mailbox.
  • RuleAction: Disable (default) keeps the rule but switches it off; Remove deletes it.
  • IncludeInboxRules: true (default) to handle inbox rules as well as mailbox forwarding; false for mailbox forwarding only.

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

remove-external-mail-forwarding.ps1 · 1174 lines · 66.2 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.