Cloudtip – Enabling User Email Forwarding

There are cases where a user leaves the company and he/she was working on specific projects or kept in touch with important contacts or clients. Those contacts don’t know that the user left the company and keep sending emails to that user. How do you send these emails to the new responsible person for those projects/clients?
That is exactly here where email forwarding can help us to solve such a problem, among others. In this post, you will see how to:

Enable Email forwarding using Office 365 portal
Disable Email Forwarding
Enable Email forwarding using Powershell
Disable Email forwarding using Powershell
Remove all Email forwarding rules from the mailboxes
Remove all Email forwarding rules with specific domains

Enable Email forwarding using Office 365 portal

Go to Office.com and select Admin portal:

Figure 1: Admin Portal

On the left-hand side, select Users, Active Users:

Figure 2: Left Panel

Search for the user, and click on its name:

Figure 3: Searching for the User

Select Mail tab, and on the Email Forwarding section, select Manage email forwarding:

Figure 4: User’s Options

Here is where you configure the forwarding itself, select the first box forward all emails sent to this mailbox to see the other options, enter the email address you want to receive the forwarded emails and check the box if you want to keep a copy of the forwarded email in the user mailbox, this is useful when the user is not disabled yet, and still working on the company for example.

Figure 5: Forwarding email tab

Note that you can have only one email address to receive the forwarding and the mailbox owner will see the changes in the forwarding settings, so be careful when you do that.


Disable Email Forwarding

To disable email forwarding, simply uncheck the forward all emails sent to this mailbox box:

Figure 6: Disabling email forwarding

Enable Email forwarding using Powershell

The command itself to set the rule is really simple:

$user = "firstname.lastname@company.com"
$ForwardedUserEmail = "firstname.lastname@company.com"
Set-Mailbox $user  -ForwardingSmtpAddress:$ForwardedUserEmail -Verbose

But in order to use it, you have to install the Exo Module and log in to it. Here is the command to install the module:

Install-Module -Name Microsoft.Exchange.Management.ExoPowershellModule -Confirm:$false

For your convenience, here is a script to automate the steps performed above:

$user = "firstname.lastname@company.com"
$ForwardedUserEmail = "firstname.lastname@company.com"

#Exchange credentials
try {
    $IsActiveSession = (Get-Mailbox -ResultSize 1 -WarningAction silentlyContinue ).name.length -gt 0
}
catch {

}

#If the section is active, just print a message, if not, install the exo module to manage exchange as admin
if ($IsActiveSession) {

    write-host "[INFO]EXO Session OK." -foregroundcolor green
}
else {

    clear-host
    Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Install-Module -Name Microsoft.Exchange.Management.ExoPowershellModule -Confirm:$false
    Write-host "Connecting to the Exchange Service, 
    please insert your credentials in the window that can be behind the current one..." -foregroundcolor yellow
    $SesionO365 = New-EXOPSSession
    Import-PSSession $SesionO365 -AllowClobber
}


#Forward the emails from this mailbox to another
Write-Host "Configuring email forwarding from $user to $forwardedUserEmail..." -ForegroundColor Yellow
Set-Mailbox $user  -ForwardingSmtpAddress:$ForwardedUserEmail -Verbose

#Checking if the rule has been applied successfully
$forwarding = (Get-Mailbox $user).forwardingSmtpAddress

if ($forwarding -eq "smtp:$forwardedUserEmail") {
    Write-Host "Forwarding rule successfully applied." -ForegroundColor Green
}
else {
    write-host "Forwarding rule not applied, please check both mailboxes addresses and try again." -ForegroundColor Red
}

This script will validate if the module is installed, then ask for Exchange credentials, set the forwarding rule and validate whether the rule has been applied or not. You can also find it on my Github account.


Disable Email forwarding using Powershell

This is the one line command to remove the rule we’ve created one step before, you can re-use the script above by replacing the 32 line and adding the following:

Set-Mailbox $user -ForwardingAddress $NULL -ForwardingSmtpAddress $NULL

Full script here.


Remove all Email forwarding rules from the mailboxes

There are tasks that are not practical to be done by using only the GUI, let’s say we have 100 users with forwarding rule applied and we need to remove their email forwarding rules. The script below can help us with it:

Get-Mailbox -ResultSize Unlimited | 
  Where {($_.ForwardingSmtpAddress -ne $NULL)} |
  Set-Mailbox -ForwardingSmtpAddress $null

Remove all Email forwarding rules with specific domains

Supposing you have accounts created for external users and those accounts had a forwarding rule to the partner domain, your company is not working with this partner anymore and you have to remove all the mailbox that were forwarding emails to that domain:

Get-Mailbox -ResultSize Unlimited | 
  Where {($_.ForwardingSmtpAddress -ne $NULL) -and $_.ForwardingSmtpAddress -like "*company.com"} |
  Set-Mailbox -ForwardingSmtpAddress $null

In this post, we tried to bring you a few cases where the forwarding rule is useful and how we can automate it to help admins on their daily tasks. You can refer to the scripts used on this post on my Github account.

Leave a comment