Removing an Office 365 domain the hard way...
We should cover all scenarios. Let’s assume that this tenant had the synchronization in place, but it was discontinued and we can’t restore the synchronization to its glory.
A good example happened to me when writing this article (the original idea of this article was to manage Office 365 domains), but I had such a hard time to remove a domain from an old subscription that I used for testing that I decided to focus just on the removal process.
If you are in this situation, the best way is using our old good friend PowerShell to remove the objects, and then return to the Office 365 Admin Console to remove the domain.
The first step is to install the Microsoft Online module in PowerShell, we can do that by using the following command line and hitting Y when requested.
Install-Module MSOnline
The second step, is to import the module that we have just installed using the cmdlet below. To check the load modules, we can use
Import-Module MSOnline
Get-Module
Connect-MSOLService
After importing the module, we need to connect to the service, we will use the Connect-MSOLService cmdlet and fill out the credentials.
We know for a fact that we had synchronization going between that subscription and our on-premises environment, and we can check if it is still enabled running the following cmdlet.
Get-MSOLCompanyInformation | Select DirectorySynchronizationEnabled
Before starting the process to remove the objects, we need to stop the synchronization configuration. We can do that by running the following cmdlet, and we can repeat the previous cmdlet to check the change on the status of the replication.
Set-MSOLDirSyncEnabled –EnableDirSync $False
When running Get-MSOLUser cmdlet we will have a list of all users of the subscription, we can check that there are some using the domain that we want to remove @infralab.org and some using the default domain which ends with .onmicrosoft.com.
We want to check all accounts that are using the domain that we are planning to remove, and to do that we can use the following cmdlet.
Get-MSOLUser | Where-Object { $_.UserPrincipalName –like “*infralab.org” }
We can remove those accounts using Office 365 Admin Center, or PowerShell, an easy way using PowerShell is adding | Remove-MSOLUser to the end of the previous cmdlet and that will start deleting those accounts associated to the infralab.org domain (a confirmation will be required).
At this point in the game, we don’t have more objects associated to the domain, so we can go back to the Office 365 Admin Center, click on Setup, and then Domains. Click on the domain, and on the new blade, click on Remove.
Now, the experience will be different, the wizard will check and no dependency will be listed because we took care in the previous steps, just click Remove, and the result will be the message bar informing that Domain was successfully removed, click Close.
And the best way is removing the domain with the powershell command: Remove-MsolDomain -DomainName “contoso.com” -Force
Steps to run in short:
Install-Module MSOnline
Import-Module
MSOnlineGet-Module
Connect-MSOLService
Get-MSOLCompanyInformation | Select DirectorySynchronizationEnabled
Set-MSOLDirSyncEnabled –EnableDirSync $False
Get-MSOLUser | Where-Object { $_.UserPrincipalName –like “*infralab.org” }
Remove-MsolDomain -DomainName “contoso.com” -Force