One common task that administrators may need to perform is identifying mailboxes that have forwarding enabled. This can be done using a PowerShell script, which we will explain in this “How to” article. We will cover the purpose of the script and explain each part of the script so you can use it to generate reports for your organization.
The Purpose of the Script
The purpose of this PowerShell script is to generate a report of all mailboxes that have forwarding enabled in Exchange Online. The script will create a CSV file that contains two columns: “Mailbox” and “ForwardTo”, which will list the user principal name of each mailbox and the email address to which emails are being forwarded, respectively. This report can be used to identify any mailboxes that have forwarding enabled and ensure that it is being used appropriately.
# Connect to your Office 365 account Connect-ExchangeOnline -UserPrincipalName <your email address> # Get a list of all mailboxes in your organization $Mailboxes = Get-Mailbox -ResultSize Unlimited # Initialize an array to hold the results $Results = @() # Loop through each mailbox and add the current forwarders, if any, to the results array foreach ($Mailbox in $Mailboxes) { if ($Mailbox.ForwardingSmtpAddress) { $Result = [PSCustomObject]@{ Mailbox = $Mailbox.UserPrincipalName ForwardTo = $Mailbox.ForwardingSmtpAddress } $Results += $Result } } # Output the results to a CSV file $Results | Export-Csv -Path "C:\forwarding_report.csv" -NoTypeInformation
Explaining the Script Parts
- Connect to Exchange Online The script starts by connecting to Exchange Online using the Connect-ExchangeOnline cmdlet. This cmdlet will prompt you to enter your Office 365 credentials. Once you have entered your credentials, the script will connect to Exchange Online.
- Get a list of all mailboxes The script uses the Get-Mailbox cmdlet to get a list of all mailboxes in the organization. The “-ResultSize Unlimited” parameter ensures that all mailboxes are included in the results.
- Initialize an array to hold the results The script initializes an array to hold the results of the report.
- Loop through each mailbox and check if forwarding is enabled The script uses a foreach loop to loop through each mailbox in the organization. It checks if forwarding is enabled for each mailbox by using the “$Mailbox.ForwardingSmtpAddress” property. If forwarding is enabled, the script creates a PowerShell custom object with two properties: the mailbox’s user principal name and the email address that emails are being forwarded to. The script then adds this object to the results array.
- Export the results to a CSV file The script uses the Export-Csv cmdlet to export the results array to a CSV file. The “-Path” parameter specifies the path where the CSV file will be saved. The “-NoTypeInformation” parameter removes the type information from the CSV file so that it can be easily opened in a spreadsheet program.
Running the Script
To run the script, follow these steps:
- Open PowerShell ISE or PowerShell command prompt as an administrator.
- Copy and paste the script into the PowerShell console.
- Replace the “<your email address>” with your own email address.
- Run the script.
The script will generate a report of all mailboxes that have forwarding enabled in Exchange Online and save it to a CSV file. The CSV file will be saved in the location specified in the script.
This PowerShell script can be a useful tool for Exchange Online administrators to identify any mailboxes that have forwarding enabled and ensure that it is being used appropriately. By generating a report of all mailboxes with forwarding enabled, administrators can take steps to manage the feature and minimize security risks. With the explanation of the script parts and running the script, you can use this script to generate reports for your organization.