Get Report on Inbox Rules set to forward or redirect emails

Sunil ChauhanConsultant
CERTIFIED EXPERT
Expertise in EXO, EOP, AzureAD, Microsoft 365 Defender, Security and Compliance, and PowerShell scripting.
Published:
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.

Below a sample report generated by the powershell script below.


Sample report:


Mailbox ForwardTo RedirectTo
test user1 c1@yahoo.com  n/a
test user2 c2@gmail.com  n/a
test user3
c3@hotmail.com myid@yahoo.com 


The script will also show its progress, as its run, like below.


1 : Getting Rules For User Mailbox: user1
2 : Getting Rules For User Mailbox: user2
3 : Getting Rules For User Mailbox: user3
4 : Getting Rules For User Mailbox: user4
5 : Getting Rules For User Mailbox: user5


This script has been used in Office 365 environment to generate the report. If you wish to run it on an on-premises Exchange environment, then I would suggest testing it first on a small batch of inboxes to confirm that its working as expected.


Please find the full script code below.


#====================================================
# Get-InboxRule-External-and-RedirectTo.ps1
# Author: Sunil Chauhan
# Email:Sunilkms@gmail.com
# website:sunil-Chauhan.blogspot.com
# This scripts gets all the forwarding and redirectTo Rules and `
# Create a Report in a presentable format.
#====================================================
#Edit report Name Below.
$reportName="Forwarding-Rules-Report.csv"

#Getting All Mailbox in the Environment
Write-host "Getting All Mailboxes.."
$AllMailboxes = Get-Mailbox -resultSize unlimited

#if you Just need to Target User Mailbox only untag the below like and tag the one above.
#$AllMailboxes = Get-Mailbox -RecipientTypeDetails userMailbox -ResultSize unlimited 

#Placeholder for Rule Collection
$AllRules = @()

#counter
$c=0

#Loop through each mailbox to fetch the Inbox Rules
foreach ($mbx in $allMailboxes)
{
$c++
Write-host "$C : Getting Rules For User Mailbox:" $mbx.Alias
$mbxRules= Get-InboxRule -Mailbox $mbx.Alias
$AllRules+=$MbxRules
}

#Filtering Forwarding Rules
$Rules = $AllRules | ? {$_.Description -match "@" -and $_.ForwardTo -ne $null -or $_.RedirectTo -ne $null}

#Placeholder for saving Report Data
$RulesDATA=@()

#Run Through Each Rule to prelare Report
foreach ($rule in $Rules)
{
#Getting Data from RedirectTo Rule
if ($rule.ForwardTo)
{
if (($rule.ForwardTo).Count   -gt 1)
{
foreach ($entry in $rule.ForwardTo)
{
if ($entry -match "@")
{
 $RulesD= New-Object -TypeName PSObject
 $RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
 $RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value $($entry | % {$($_.split("[")[0]).Replace('"',"")})
 $RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value "n/a"
 $RulesDATA+=$rulesD
 }
}
}
Else{
if ($rule.ForwardTo -match "@")
{
 $RulesD= New-Object -TypeName PSObject
 $RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
 $RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value $($rule.ForwardTo  | % {$($_.split("[")[0]).Replace('"',"")})
 $RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value "n/a"
 $RulesDATA+=$rulesD
}
   }
}
#Getting Data from RedirectTo Rule
if ($rule.RedirectTo)
{
if (($rule.RedirectTo).Count   -gt 1)
{
foreach ($entry in $rule.RedirectTo)
{
 if ($entry -match "@") 
{
 $RulesD= New-Object -TypeName PSObject
 $RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
 $RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value n/a
 $RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value $($entry  | % {$($_.split("[")[0]).Replace('"',"")})
 $RulesDATA+=$rulesD
}
}
}
Else{
if ($rule.RedirectTo -match "@") 
{
 $RulesD= New-Object -TypeName PSObject
 $RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
 $RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value "N/A"
 $RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value $($rule.RedirectTo  | % {$($_.split("[")[0]).Replace('"',"")})
 $RulesDATA+=$rulesD 
 }
}
}
}

#exporting report Data to Csv File
$RulesDATA | Export-Csv $reportName -notype


Note: Please make sure to test the PowerShell script for a small number of mailboxes to confirm if it's generated the expected report.


Please feel free to leave your feedback or issues regarding the script in the comments section I will try to resolve them.

1
9,413 Views
Sunil ChauhanConsultant
CERTIFIED EXPERT
Expertise in EXO, EOP, AzureAD, Microsoft 365 Defender, Security and Compliance, and PowerShell scripting.

Comments (5)

Albert WidjajaIT Professional
CERTIFIED EXPERT

Commented:
Hi Sunil,

Does this means that if the Outlook client is closed, the result can still be displayed or it will be skipped ?
Sunil ChauhanConsultant
CERTIFIED EXPERT

Author

Commented:
this script gets the server side rule, so no impact of outlook open or closed.
Hi,

Thank you for the rule . we are testing this in our environment. Do you think if you have any script to display the full access permission for all the mailboxes
Sunil ChauhanConsultant
CERTIFIED EXPERT

Author

Commented:
still inbox rule executing and did not provide any result. Hope it will complete and some results. we are running for office365

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.