Spending hours of your life trying to manually remove forwarding addresses from Exchange or O365 mailboxes is a thing of the past thanks to PowerShell and its multi user editing super powers. In this PowerShell tutorial, I'll show you the quick easy way to blast those nasty email forwards right out of existence.

The two types of mailbox forwards

There are two types of mailbox forward - user level and administrator level. The difference is fairly self explanatory with user level forwards being set by the users (although administrators have the power to do this too) and administrator level forwards being set only by administrators. This tutorial will show you how to remove both kinds. It's important to note which type (if not both) that you are trying to remove.

Open a remote powershell session to O365

First, we're going to unleash the awesome and fearful power of PowerShell through a remote PowerShell session so we can prep everything for the magic that's about to come. Open up a fresh PowerShell ISE session and run the following command:

Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy Unrestricted
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session 
Connect-MsolService
Import-Module Msonline

Review all forwards that are currently in place

Before removing the forwards, it's important to check and make sure we're not deleting anything important that someone else has setup and that we have a backup with the forward information in case we need it. To do this, we'll run the script below to output a list of all forwards for all of the users in the O365 environment:

Get-mailbox | Where {($_.ForwardingAddress -ne $Null) -or ($_.ForwardingsmtpAddress -ne $Null)} | 
Select Name, ForwardingAddress, ForwardingsmtpAddress, DeliverToMailboxAndForward | 
Export-Csv "listfwds.csv" -NoTypeInformation
Write-Host "$ forwarding address has been output for ($user.UPN)"

Create a new CSV with the users you want to strip forwards from

Using the list we just exported, we can now do a quick cleanup of any users we don't want to remove the forwards from by deleting unwanted rows. Once the list is cleaned up, delete all of the columns except for the one with the PrimarySmtpAddress header and rename the header to UPN.

Now that you've completed cleaning up your template so that only the users whose forwards you want to remove are included, it's time to save it as a new CSV a file. For this tutorial, I'll use the hypothetical target location of C:\Users\Ayrn\Documents and name the file gbfwds.csv.

Narrowly avert disaster by performing a few simple checks

To avoid going full on Rambo in the next steps, let's first CD to the directory that the CSV file we created earlier lives happily in:

cd "C:\Users\Ayrn\Documents"

To make sure you're not about to annihilate hundreds of forwards that you don't intend to by evoking the wrong csv file, let's check to make sure we're in the right location using our good ol' friend, the ls command:

ls

It should return something that looks like the output below. The important part here is to make sure that the directory is correct and that the file is in there.

PS C:\Users\ExampleUser\Documents> ls

    Directory: C:\Users\Ayrn\Documents
    
Mode                LastWriteTime         Length Name
----                -------------         ------ ----     
-a----        1/28/2020  10:51 PM         3973   gbfwds.csv 

To remove administrator forwards from multiple users

Now that we've confirmed we're not headed straight for a career ending event, it's time to get down and dirty with our script. Take a deep breath and try to imagine what absolute bliss you're going to feel watching hundreds or maybe even thousands of email forwards go away line by line, minute by minute, maybe even hour by hour. Ready? Ok, so now, assuming you want to remove administrator forwards, you'll run this:

$users = import-csv '.\gbfwds.csv'
foreach ($user in $users)
{
	Set-Mailbox $user.UPN -ForwardingSmtpAddress $Null
	Write-Host "$ forwarding address removed from ($user.UPN)"
}
$users = $null

To remove user level forwards from multiple users

If you would like to blast away user level forwards the only difference will be that we'll substitute -ForwardingSmtpAddress with ForwardingAddress:

$users = import-csv '.\gbfwds.csv'
foreach ($user in $users)
{
	Set-Mailbox $user.UPN -ForwardingAddress $Null
	Write-Host "$ forwarding address removed from ($user.UPN)"
}
$users = $null

In either case, if all goes well, you should see success messages that look something like this:

forwarding address removed from [email protected]

That's it, you're done! Rinse and repeat if necessary.

Share this post