Security · 1 tool

Find mailboxes that forward mail outside the company

Summary

Finds forwarding set on the mailbox and in inbox rules that sends mail to external addresses, one of the most common signs of a compromised account and a common leftover after a migration.

Implementation

Choose your tool. Your choice is remembered on other samples.

Exchange Online PowerShell
Connect-ExchangeOnline -ShowBanner:$false
$accepted = (Get-AcceptedDomain).DomainName
function Test-External($addr) { $d = ($addr -replace "^smtp:", "" -split "@")[-1]; $d -and ($accepted -notcontains $d) }

Get-EXOMailbox -ResultSize Unlimited -Properties ForwardingSmtpAddress, DeliverToMailboxAndForward |
  Where-Object { $_.ForwardingSmtpAddress -and (Test-External $_.ForwardingSmtpAddress) } |
  Select-Object PrimarySmtpAddress, ForwardingSmtpAddress, DeliverToMailboxAndForward |
  Export-Csv .\mailbox-forwarding.csv -NoTypeInformation

Get-EXOMailbox -ResultSize Unlimited | ForEach-Object {
  $mbx = $_.PrimarySmtpAddress
  Get-InboxRule -Mailbox $mbx -ErrorAction SilentlyContinue |
    Where-Object { $_.ForwardTo -or $_.RedirectTo -or $_.ForwardAsAttachmentTo } |
    Select-Object @{ n = "Mailbox"; e = { $mbx } }, Name, Enabled, ForwardTo, RedirectTo, ForwardAsAttachmentTo
} | Export-Csv .\inbox-rule-forwarding.csv -NoTypeInformation

Prerequisites

  • Exchange Administrator
  • ExchangeOnlineManagement module 3.x

Output

Two CSVs: mailbox-level forwarding and inbox rules that forward or redirect.

Contributors

AuthorRole
Priyanshu UpadhyaySolution Architect, Microsoft 365 and Azure
Disclaimer. These samples are provided as is, without warranty of any kind. Read and test every script in a non-production tenant before you run it, and run reports before any command that changes or deletes data.