Recover when a mailbox exists in both Exchange Online and on-premises

Summary of issue:

In a Microsoft Exchange Server hybrid deployment, a user may have a mailbox in both Exchange Online and an Exchange on-premises organization. This isn't a desired state for a hybrid organization because it will create mail flow issues. In this case, messages will be delivered to the mailbox that corresponds to the location of the sender. If the sender is located in your on-premises organization, messages will be delivered to the on-premises mailbox. If the sender is located in your Exchange Online tenant, messages will be delivered to the Exchange Online mailbox.

Note : The PowerShell commands below are run from your on-premises Exchange, not from Exchange Online.

In this instance we would like to keep the Exchange Online mailbox and discard the on-premises mailbox, to accomplish this, you need to follow this:

Open the Exchange Management Shell, save the on-premises mailbox information to a file, such as "SMTP addresses", "Legacy Exchange DN", "Exchange attributes", and so on.

$formatenumerationlimit = -1
Get-Mailbox "mailbox identity" | fl > mailboxinfo.txt

Disconnect the on-premises mailbox:

Disable-Mailbox "Tech.Bear@pokebearswithsticks.com"

Enable the on-premises user as a remote mailbox:

Enable-RemoteMailbox "Tech.Bear@pokebearswithsticks.com" -RemoteRoutingAddress "TechBear@pokebearswithsticks.mail.onmicrosoft.com"

Restore any custom proxy addresses and any other Exchange Server attributes that were stripped when the mailbox was disabled, in my case I required Tech.Bear@pokebearswithsticks.com adding back in, but you can check the mailboxinfo.txt file under the attribute "EmailAddresses" - do not add the X500 address here this is done later.

Add the LegacyExchangeDN value of the previous on-premises mailbox to the proxy address of the new remote mailbox as an x500 address. To do this, run the following cmdlet:

Set-RemoteMailbox -Identity "user identity" -EmailAddresses @{add="x500:/o=Bears of the World/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Tech.Bear"}

Stamp the Exchange Online GUID on the remote mailbox using Exchange Management shell, to complete this connect to Exchange Online, if you need to use a proxy then add the line in BOLD, if you do not need a proxy skip it:

$ProxyOptions = New-PSSessionOption -ProxyAccessType ieconfig
Connect-ExchangeOnline -PSSessionOption $ProxyOptions

Then once connected run this:

Get-EXOMailbox Tech.Bear@pokebearswithsticks.com

When that command runs the last line will look like this

Guid                      : 2e14f32b-0f9b-400b-9f07-7439a8e36e6e

WARNING: This is the GUID from EXO not local Exchange, the one in the file from earlier is the wrong GUID

Now you have the EXO mailbox GUID you can link that the mailbox using this command:

Set-RemoteMailbox "user identity" -ExchangeGuid "2e14f32b-0f9b-400b-9f07-7439a8e36e6e"

Once the AD Connect replication next runs automatically, you are all good.