In the current article, we will review how to use the PowerShell commands for managing…
Manage Forward Mail with PowerShell | Office 365
In the current article, we review the use of the Forward email PowerShell command in Office 365 and Exchange Online environment.
Table of contents
Introduction
Forward email rule as the name implies, enable to forward automatically each email that sent to a particular mailbox to an additional email address.
The “Destination recipient” (E-mail address) could be “internal Mailbox” meaning a recipient from our organization or external recipient, meaning – recipient doesn’t belong to the organization (there is a different PowerShell Parameter for each of the options).
An additional parameter is an option of leaving a copy of the email in the source recipient mailbox or just forward the email message.
In this article, we will demonstrate a couple of scenarios for Mail Forwarding:
- Mail forwarding to an internal recipient: John and Suzan are recipients who belong to the office 365 domain name o365info.com.
- Mail forwarding to External recipient: John wants to forward each mail he gets to an external email address DavidWilkins@hotmail.com.
- Additional scenarios are: Mail Forwarding in Bulk mode – the option of forwarding email to a destination email address for all the office 365 users and a nice trick of email forwarding that will implement for external contact object.
Connect to Exchange Online PowerShell
To be able to run the PowerShell commands specified in the current article, you will need to connect to Exchange Online PowerShell.
Start Windows PowerShell as administrator and run the cmdlet Connect-ExchangeOnline.
Connect-ExchangeOnline
1. Set mail forwarding
ADMIN Forwarding (ForwardingAddress)
ADMIN Forwarding (ForwardingAddress) – Forward Email to Recipient & SAVE local copy (Default)
PowerShell command syntax:
Set-Mailbox -ForwardingAddress
PowerShell command example:
Set-Mailbox "Angelina" -ForwardingAddress "Jacob.Peters@o365info.com"
ADMIN Forwarding (ForwardingAddress) – Forward Email to Recipient DONT SAVE a local copy
PowerShell command syntax:
Set-Mailbox -ForwardingAddress -DeliverToMailboxAndForward $False
PowerShell command example:
Set-Mailbox "Angelina" -ForwardingAddress "Jacob.Peters@o365info.com" -DeliverToMailboxAndForward $False
USER Forwarding (ForwardingsmtpAddress)
Admin Forwarding (ForwardingsmtpAddress) – Forward Email to Recipient & SAVE local copy (Default)
PowerShell command syntax:
Set-Mailbox -ForwardingsmtpAddress
PowerShell command example:
Set-Mailbox "Angelina" -ForwardingsmtpAddress "Jacob.Peters@o365info.com"
Admin Forwarding (ForwardingsmtpAddress) – Forward Email to Recipient DONT SAVE a local copy
PowerShell command syntax:
Set-Mailbox -ForwardingsmtpAddress -DeliverToMailboxAndForward $False
PowerShell command example:
Set-Mailbox "Angelina" -ForwardingsmtpAddress "Jacob.Peters@o365info.com" -DeliverToMailboxAndForward $False
Create External contact with internal email address + Forward email address (External Email Address)
Step 1: Create External contact with External email address.
PowerShell command syntax:
New-MailContact -Name "Display Name" -ExternalEmailAddress "External Recipient Email Address"
PowerShell command example:
New-MailContact -Name "David Wilkins" -ExternalEmailAddress "DavidWilkins@hotmail.com"
Step 2: Set External contact email address to internal email address and forwarding email address (External email address).
PowerShell command syntax:
New-MailContact "Display Name" -EmailAddresses "SMTP:<Office 365 User Email Address>,<External Recipient Email Address>"
PowerShell command example:
Set-MailContact "David Wilkins" -EmailAddresses "SMTP:David.Wilkins@o365info.com,DavidWilkins@hotmail.com"
2. Display information about Specific Mailbox Forwarding settings
PowerShell command syntax:
Get-Mailbox | FL DeliverToMailboxAndForward,ForwardingAddress,ForwardingSmtpAddress
PowerShell command example:
Get-Mailbox "Jacob.Peters@o365info.com" | FL DeliverToMailboxAndForward,ForwardingAddress,ForwardingSmtpAddress
Find all Recipients (Display list) with ADMIN Forwarding or USER Forwarding
PowerShell command example:
Get-Mailbox -ResultSize Unlimited | Where {($_.ForwardingAddress -ne $Null) -or ($_.ForwardingsmtpAddress -ne $Null)} | Select Name, ForwardingAddress, ForwardingsmtpAddress, DeliverToMailboxAndForward
3. Disable (remove) E-mail Forwarding option
Disable (cancel) ADMIN Forwarding (ForwardingAddress) Specific MAILBOX
PowerShell command syntax:
Set-Mailbox -ForwardingAddress $Null
PowerShell command example:
Set-Mailbox "Jacob.Peters@o365info.com" -ForwardingAddress $Null
Disable (cancel) USER Forwarding (ForwardingsmtpAddress) Specific MAILBOX
PowerShell command syntax:
Set-Mailbox -ForwardingSmtpAddress $Null
PowerShell command example:
Set-Mailbox "Jacob.Peters@o365info.com" -ForwardingSmtpAddress $Null
Disable (cancel) ADMIN Forwarding (ForwardingAddress) ALL MAILBOXES (BULK mode)
PowerShell command example:
Get-Mailbox -ResultSize Unlimited| Where-Object {($_.ForwardingAddress -ne $Null) } | Set-Mailbox -ForwardingAddress $Null
Disable (cancel) USER Forwarding (ForwardingsmtpAddress) ALL MAILBOXES (BULK mode)
PowerShell command example:
Get-Mailbox -ResultSize Unlimited | Where-Object {($_.ForwardingsmtpAddress -ne $Null) } | Set-Mailbox -ForwardingsmtpAddress $Null
4. Export information about forwarding settings
Export information about USER Forwarding rule
Export to CSV file.
PowerShell command example:
Get-Mailbox -ResultSize Unlimited | Where-Object {($_.ForwardingsmtpAddress -ne $Null) } | Select-Object DisplayName,Alias, PrimarySmtpAddress, ForwardingAddress, ForwardingsmtpAddress, DeliverToMailboxAndForward, RecipientType,RecipientTypeDetails | Export-CSV C:\TEMP\"All Recipents that have USER Forwarding(ForwardingsmtpAddress).CSV" -NoTypeInformation -Encoding UTF8
Export information about ADMIN Forwarding rule
Export to CSV file.
PowerShell command example:
$fwds = Get-Mailbox -ResultSize Unlimited | Where-Object { $_.ForwardingAddress -ne $null } | Select-Object DisplayName,Alias, name, ForwardingAddress
; foreach ($fwd in $fwds) {$fwd | add-member -membertype noteproperty -name "ContactAddress" -value (get-contact $fwd.ForwardingAddress).WindowsEmailAddress} ; $fwds | Export-CSV C:\TEMP\"All Recipents that have Admin Forwarding(ForwardingAddress).CSV" -NoTypeInformation -Encoding UTF8
Export information about User inbox Forwarding rule
Export to CSV file.
PowerShell command example:
$UserInboxRule = ForEach ($i in (Get-Mailbox -ResultSize Unlimited)) {Get-InboxRule -Mailbox $i.DistinguishedName | Where-Object {$_.ForwardTo} | fl MailboxOwnerID,Name,ForwardTo,Description} $UserInboxRule | Out-File C:\TEMP\"User inbox Forwarding rule.txt" -Encoding UTF8
5. Forward Email of ALL Users to Additional email address (Bulk mode)
Forward Email of ALL Users to internal Recipient & save local copy
Step 1. Save local copy.
PowerShell command syntax:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"} | Set-Mailbox -DeliverToMailboxAndForward $True
Step 2. Forward email to the destination recipient (internal\organization recipient).
PowerShell command syntax:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"} | Set-Mailbox -DeliverToMailboxAndForward $True
PowerShell command example:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"} | Set-Mailbox -ForwardingAddress Suzan@o365info.com
Forward Email of ALL Users to External Recipient & save local copy
Step 1. Save local copy.
PowerShell command syntax:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"}| Set-Mailbox -DeliverToMailboxAndForward $True
PowerShell command example:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"}| Set-Mailbox -DeliverToMailboxAndForward $True
Step 2. Forward email to the destination recipient (internal\organization recipient).
PowerShell command syntax:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"}| Set-Mailbox -ForwardingSmtpAddress <External Recipient Email Address>
PowerShell command example:
Get-Mailbox | Where {$_.RecipientType -eq "UserMailbox"} | Set-Mailbox -ForwardingSmtpAddress Suzan@hotmail.com
Forward Email to External Recipient & save local copy | import from CSV File
Scenario 1:
Step 1. Enable the option DeliverToMailboxAndForward.
$Mailboxes= Import-CSV C:\Temp\Users.csv
ForEach ($Mailbox in $Mailboxes)
{Set-Mailbox $Mailbox.users -DeliverToMailboxAndForward $True}
Step 2. Forward email to the destination recipient (internal\organization recipient).
$Mailboxes= Import-CSV C:\Temp\Users.csv
ForEach ($Mailbox in $Mailboxes)
{Set-Mailbox $Mailbox.users -ForwardingAddress Suzan@o365info.com}
CSV file example:
Scenario 2:
Enable the option DeliverToMailboxAndForward.
Import-CSV "C:\Temp\Users.csv" | % { $_.Condition = [bool]($_.Condition -as [int]); $_ } | ForEach {Set-Mailbox -Identity $_.mailbox -ForwardingAddress $_.forwardto -Delivertomailboxandforward $_.Condition}
CSV file example:
Still helping! Thank you!
Thank you so so much!
#appreciate
I actually adore your post. I read your site pretty regularly and you are always coming out with some
great items. I shared this on my FB and my followers only loved it.
Keep up the good work!
Great Article. Is there a way to turn on forwarding for a mailbox and it will stop forwarding after 60 days?
Oh my goodness! Amazing article dude! Many thanks!
Thank you so much, I used “2.2 – Find all users with Forwarding Address is set to Internal Recipient” to find an old mailbox that was causing problems for us! Very nice resource you have with this page.
How come -ForwardingSMTPAddress should be used to forward to external email addresses, but the script in section 4.4 “Forward Email to External Recipient & save local copy | import from CSV File” uses -ForwardingAddress
Import-CSV “C:\Temp\Users.csv” | % { $_.Condition = [bool]($_.Condition -as [int]); $_ } | ForEach {Set-Mailbox -Identity $_.mailbox -ForwardingAddress $_.forwardto -Delivertomailboxandforward $_.Condition}
Is this just an error in the script? Or is there a reason for this?
Can someone help me with a script to Forward Email to External Recipient & DONT SAVE local copy | import from CSV File
Thanks to the comments above this is the command that I found to work for “Forwarding Email to External Recipient & save local copy | import from CSV File |Scenario 2”:
Import-CSV “C:\Temp\Users.csv” | % { $_.Condition = [bool]($_.Condition -as [int]); $_ } | ForEach {Set-Mailbox -Identity $_.mailbox -ForwardingSmtpAddress $_.forwardto -Delivertomailboxandforward $TRUE}
Wow, great resource. I was just thrown into the mix with O365, and this helps immensely!
How do I forward e-mails from a specific domain to another mailbox?
Very helpful thank you so much for sharing.
“Forward Email to External Recipient & DONT save local copy” => Does this really work? In our case we failed with tests using ForwardingsmtpAddress and what I could read in other discussions our experience was confirmed.
Forward Email to internal Recipient & save local copy (Default)
Is not the default setting in my office365 config.
The default is Forward Email to internal Recipient & DONT save local copy
So the command will be Set-Mailbox -Identity -ForwardingAddress -DeliverToMailboxAndForward $true
-ForwardingAddress (internal; within organtization), -ForwardingSmtpAddress (external; forward to an email address outside of your organization)
Great article which makes things easy.
But what is the difference between -ForwardingAddress and -ForwardingSmtpAddress ???