<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Get Report on Inbox Rules set to forward or redirect emails

Published on
7,250 Points
4,150 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
Comment
  • 2
  • 2
5 Comments
LVL 9

Expert Comment

by:Senior IT System Engineer
Hi Sunil,

Does this means that if the Outlook client is closed, the result can still be displayed or it will be skipped ?
0
LVL 14

Author Comment

by:Sunil Chauhan
this script gets the server side rule, so no impact of outlook open or closed.
0

Expert Comment

by:sankara parameswaran
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
0
LVL 14

Author Comment

by:Sunil Chauhan
0

Expert Comment

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

Featured Post

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Join & Write a Comment

Screencast - Getting to Know the Pipeline
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month