Remove a custom domain from Office 365 - Azure Active Directory.

I was setting up a custom domain within Azure, and after I add the TXT record to our DNS to verify ownership. It gave me the following error message.

To do this in PowerShell we’re going to install the Azure ActiveDirectory (MSOnline) Module

install-module MSOnline

Connect-MsolService

Connect-MsolService

Remove-MsolDomain

Remove-MSolDomain -DomainName yourdomain.com

I was greeted with the following error:

I then ran the Get-MsolUser command as the error suggested

Get-MsolUser -DomainName yourdomain.com

But this returned nothing.

You need to remove the users, groups and XXXX before being able to remove the domain.

View and remove groups

Within the Office 365 portal I wasn’t able to delete the group that existed for the domain.

View groups

But by running the Get-MsolGroup command I can see all the groups in the account

Get-MsolGroup

I can see the group that exists within the domain. And can take note of the objectid to use with the remove command Remove-MsolGroup

Remove the group object
Remove-MsolGroup -Objectid '9b1aer67-11x4-4398-b8cb-330b923f3016'
Delete the domain

Now we have deleted all the resources in the domain we can again use the Remove-MsolDomain command to delete our Domain.

Remove-MSolDomain -DomainName contoso.com