<

Get Report on Inbox Rules set to forward or redirect emails

Published on
9,416 Points
6,316 Views
1 Endorsement
Last Modified:
Sunil Chauhan
Expertise in Microsoft Exchange Server, Office 365, and PowerShell scripting, In my spare time I love writing tech blogs and taking photos.
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
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free