<

Go Premium for a chance to win a PS4. Enter to Win

x

Get Report on Inbox Rules set to forward or redirect emails

Published on
3,788 Points
688 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 8

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 10

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 10

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month