Skip to main content
Exchange Online · PowerShell 7.6

Mailboxes forwarding to external addresses

Which mailboxes have mailbox-level forwarding (ForwardingSmtpAddress or ForwardingAddress) to an address outside your accepted domains?

Script

#Requires -Modules ExchangeOnlineManagement
# Mailboxes whose mailbox-level forwarding points outside the accepted domains. Read-only.

Connect-ExchangeOnline -ShowBanner:$false

$acceptedDomains = @(Get-AcceptedDomain | ForEach-Object { $_.DomainName.ToString().ToLowerInvariant() })

function Get-AddressDomain {
    param([string]$Address)
    if (-not $Address) { return $null }
    $smtp = $Address -replace '^smtp:', ''
    return ($smtp -split '@')[-1].ToLowerInvariant()
}

Get-EXOMailbox -ResultSize Unlimited -Properties ForwardingSmtpAddress, ForwardingAddress, DeliverToMailboxAndForward |
    Where-Object { $_.ForwardingSmtpAddress -or $_.ForwardingAddress } |
    ForEach-Object {
        $target = $_.ForwardingSmtpAddress
        if (-not $target) {
            $target = (Get-EXORecipient -Identity $_.ForwardingAddress).PrimarySmtpAddress
        }
        $domain = Get-AddressDomain -Address "$target"
        [pscustomobject]@{
            Mailbox                    = $_.UserPrincipalName
            ForwardsTo                 = "$target"
            External                   = $acceptedDomains -notcontains $domain
            DeliverToMailboxAndForward = $_.DeliverToMailboxAndForward
        }
    } |
    Where-Object { $_.External } |
    Sort-Object Mailbox

Checked automatically against Microsoft's published cmdlet reference. KBY hasn't run it in your tenant; run it in a test environment first.

Reading the output

  • Checks mailbox forwarding settings only. Forwarding set up in inbox rules or transport rules is not covered.
  • A domain counts as internal only if it is listed exactly in Get-AcceptedDomain; subdomains of an accepted domain are reported as external.
  • ExchangeOnlineManagement 3.10.0 and later require PowerShell 7.6 when run in PowerShell 7 (Windows PowerShell 5.1 is unaffected), per the module release notes.

Sources

The Microsoft reference page for every cmdlet the script calls, plus the API and product pages behind each permission and caveat.

All scripts