Removing a domain from Office 365

By |2017-12-19T11:51:58+00:00November 22nd, 2017|Office 365|0 Comments

I recently came across a problem removing a domain from a office 365 tenancy. The customer was moving a SMTP domain from one office 365 tenancy to another. Users were sync’d to the source tenancy via AAD Connect. This was disabled to enable us to remove the domain from all objects in the tenancy (we wanted to keep the mailboxes as we were using a 3rd party tool to migrate the data from the source to the target mailbox. This was being done on the address due to not being able to register the domain in the target tenancy until it is removed from the source. When we went to remove the domain we received the error: Unable to remove domain. There are user accounts that are associated with this domain.

We will use as the SMTP domain in this example.

The steps involved on other domains that had worked previously were as follows:

  • Disable AAD Connect – Set-MSOLDirSyncEnabled –EnableDirSync $False
  • Change UserPrincipalName for all users. Set Users primary SMTP address to the address and remove
  • Change all Groups primary SMTP address to the address and remove
  • Change all resource mailbox primary SMTP address to the address and remove
  • Change any contacts that contain
  • Return all deleted users (Get-MsolUser -ReturnDeletedUsers -DomainName | fl UserPrincipalName) and remove

All the above was done via scripts but we still received the same error.

To resolve this we ran the following which checks ALL objects in office 365 and lists any objects that contain reference to the domain in question

$a = get-msoluser -all
$b = get-msoluser -all -returndeletedusers
$c = get-msolgroup -all
$d = get-msolcontact -all
$all = $a+$b+$c+$d
$search = “”
$all | ?{$_.emailaddresses -match $search -or $_.emailaddress -match $search -or $_.userprincipalname -eq $search -or $_.proxyaddresses -match $search}

We could then change the objects that were returned and then remove the domain from the source tenancy and add to the new target tenancy

Leave A Comment

like what you see? 

Sign-up to our newsletter and never miss out on the latest blogs, events and tech news from the world of risual
Give it a try, you can unsubscribe anytime.