Office 365, PowerShell, Technology

Exchange Online Migration error: “Target user already has a primary mailbox”

Contents

Issue

When migrating mailboxes from Exchange On Premise to Exchange Online, it fails with the following error:
Target user ‘Users Name’ already has a primary mailbox.

Cause

Long before we had ever thought about migrating to Exchange Online we had manually created some students in our Office 365 tenant (not using the Azure AD Connect) to give them access to Visual Studio Community. Unbeknownst to us, this was creating their mailboxes and going to give us some stick down the line. Before syncing these users using Azure AD Connect, we removed them from Office 365 to avoid any duplicate errors (we had experienced that before too…).

So when we synced these users to Office 365 there weren’t any issues. But when we went to migrate their mailboxes we got the error:
Target user ‘Users Name’ already has a primary mailbox.

There was obviously some link with the synced accounts to the mailboxes of the previously manually created accounts

Resolution

To fix this issue we needed to “Clear Previous Mailbox Info” for the user.

The high level steps for this are:

  1. Remove users licence
  2. Run command to clear their previous mailbox info
  3. Add users licence

 

Verify the users do have a mailbox in O365

You can probably skip this step as you already know they do if it has failed, but after coming across this issue I ran this command in preparation of our staged migration to get the list of any users that already had mailboxes and were going to cause issue.

This will loop through your list of users and get anyone with a “UserMailbox” in Office 365

foreach($user in $usersToClear)
{
Get-User $user.Name | Select name,*Recipient* | where {$_.RecipientType -like “UserMailbox”}
}

Remove the users licence

We use group assignment through groups. So to remove the users licence we needed to remove them from the appropriate group. The following PowerShell code will remove the list of users from the licence group:

foreach($user in $usersToClear)
{
Remove-ADGroupMember -Identity GroupName -Members $user.name -Confirm:$false
}

You will need to run an Azure AD sync after removing the users from the group.

If you assign licences through Office 365 (not using group assignment), you could use following line to remove the licences for the list of users:

foreach($user in $usersToClear)
{
Set-MsolUserLicense -UserPrincipalName $user.name -RemoveLicenses LicenceName
}

Clear Previous Mailbox Info

The following piee of code will clear the users previous mailbox info:

foreach($user in $usersToClear)
{
Set-User $user.name -PermanentlyClearPreviousMailboxInfo
}

You can check if this worked by running the same command as in section one of this article to get the users the have mailbox info

Add Users Licence

The last thing we need to do is give the user back the licence we removed earlier. Again, as we use group assignment we use the following code to add them to the appropriate group:

foreach($user in $usersToClear)
{
Add-ADGroupMember -Identity GroupName -Members $user.name -Confirm:$false
}

Run another Azure AD sync after this to sync the licence properties.

 

Leave a Reply

Your email address will not be published.