Skip to content

Instantly share code, notes, and snippets.

@jschlackman
Last active October 29, 2025 20:41
Show Gist options
  • Save jschlackman/d6d0183797c7556919609459f3fad3e8 to your computer and use it in GitHub Desktop.
Save jschlackman/d6d0183797c7556919609459f3fad3e8 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Retrieves all Exchange inbox rules that forward messages to another recipient.
.DESCRIPTION
Author: James Schlackman <[email protected]>
Last Modified: Oct 29 2025
.PARAMETER OutputPath
Path to optional CSV export file.
.PARAMETER Recipients
List of mailbox identities to check for rules. If not specified, all mailboxes in the tenant will be checked.
#>
#Requires -Modules @{ModuleName='ExchangeOnlineManagement'; ModuleVersion='1.0.1'}
Param(
[Parameter()] [ValidateNotNullOrEmpty()] [String] $OutputPath = ('{0:yyMMdd} Forwarded Mailboxes.csv' -f (Get-Date)),
[String[]] $Recipients
)
If (!$Recipients) {
Write-Host 'Getting list of recipients...'
$Recipients = (Get-EXOMailbox -ResultSize Unlimited).PrimarySmtpAddress | Sort-Object
}
Write-Host ('Number of mailboxes to be checked for forwarding rules: {0}' -f @($Recipients).Count)
$InboxRules = @()
$ProgressPreference = 'Continue'
$progActivity = 'Retrieving mailbox rules'
# Get rules for each mailbox
for ($index = 0; $index -lt @($Recipients).Count; $index++) {
$rcpt = @($Recipients)[$index]
Write-Progress -Activity $progActivity -Status $rcpt -PercentComplete ($index * 100 / @($Recipients).Count)
$InboxRules += Get-InboxRule -Mailbox $rcpt | Select @{Name='Mailbox';Expr={$rcpt}},Name,Enabled,ForwardTo,ForwardAsAttachmentTo,RuleIdentity
}
Write-Progress -Completed
# Filter only rules that have forwards
$resultOutput = $InboxRules | Where-Object {($_.ForwardTo -ne $null) -or ($_.ForwardAsAttachmentTo -ne $null)}
# Show in grid
Write-Host 'See grid export for details and select records for export.'
$resultExport = $resultOutput | Out-GridView -OutputMode Multiple -Title 'Select records for export to CSV'
# Export selected records to file
If ($resultExport) {
Write-Host 'Exporting to ' -NoNewline
Write-Host $OutputPath -ForegroundColor Green
$resultExport | Export-Csv -NoTypeInformation -Path $OutputPath -Encoding UTF8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment