How to remove an Office 365 domain using PowerShell

Featured image

In this blog, we share our experience in addressing the error on removing Office 365 domain for admin community.

Error during Office 365 Domain Removal:

When you try to remove an Office 365 domain using Windows PowerShell, you may get the following error message:

“Remove-MsolDomain : Unable to remove this domain. Use Get-MsolUser -DomainName to retrieve a list of objects that are blocking removal……….”

This error on removing the Office 365 domain occurs, because all the users’ UserPrincipalName and email addresses using the old domain name, so the domain will not be removed. Therefore to remove the old domain, you must first change the UserPrincipalName of all users, email addresses, or Office 365 accounts associated with the old domain.

Steps Involved to Remove an Office 365 Domain:

1 . Change the UserPrincipalName for all Office 365 users 2 . Change Email Addresses for all Office 365 Mailboxes and Groups 3 . Remove the old Office 365 domain

Before starting the process, download and install the Windows Azure Active Directory Module for Windows PowerShell and execute the following PowerShell Scripts by using Global Administrator credentials.

​1. Change the UserPrincipalName for all Office 365 users

You can skip this step if all Office 365 users have Exchange Online account.

Following PowerShell script is used to change the domain name from old domain to new domain in UserPrincipalName for all Office 365 users,

Input Parameters required: $olddomain - Office 365 domain name (old domain need to be removed) $Newdomain - Office 365 domain name (new domain need to be updated)

step 1 :Get values for input parameters:

$olddomain =”olddomainname.com”
$Newdomain="Newdomainname.com"

step 2 :Connect to MsolService:

Import-Module MsOnline
$credential = get-credential
Connect-MsolService -Credential $credential

step 3 :Change the UserPrincipalName for all Office 365 users

$users=Get-MsolUser -domain $olddomain
$users | Foreach-Object{ 
$user=$_
$UserName =($user.UserPrincipalName -split "@")[0]
$UPN= $UserName+"@"+ $Newdomain 
Set-MsolUserPrincipalName -UserPrincipalName $user.UserPrincipalName -NewUserPrincipalName $UPN
}
  1. Change Email Addresses for all Office 365 Mailboxes and Groups

Using the following PowerShell script,

Input Parameters required:

$Newdomain - Office 365 domain name (new domain need to be updated)

step 1 :Get values for input parameters::

$Newdomain="Newdomainname.com"

step 2 :Connect to Exchange Online:

$credential = get-credential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 
"https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" –AllowRedirection
Import-PSSession $ExchangeSession

step 3 :Change Email Addresses for all Office 365 Mailboxes

$Users=Get-Mailbox
$Users | Foreach-Object{ 
$user=$_
$UserName =($user.PrimarySmtpAddress -split "@")[0]
$SMTP ="SMTP:"+ $UserName +"@"+$Newdomain 
$Emailaddress=$UserName+"@"+$Newdomain
$user | Set-Mailbox -EmailAddresses $SMTP -WindowsEmailAddress $Emailaddress -MicrosoftOnlineServicesID $Emailaddress 
}

step 4 :Change Email Addresses for all Groups, below example shows changing email addresses for all distribution groups

$Groups=Get-DistributionGroup
$Groups | Foreach-Object{ 
$group=$_
$groupname =($group.PrimarySmtpAddress -split "@")[0]
$SMTP ="SMTP:"+$groupname+"@"+$Newdomain 
$Emailaddress=$groupname+"@"+$Newdomain
$group |Set-DistributionGroup -EmailAddresses $SMTP -WindowsEmailAddress $Emailaddress -MicrosoftOnlineServicesID $Emailaddress 
} 

NOTE: Similarly you can change Email Addresses for all Dynamic Distribution Groups and Office 365 Groups.

​3. Remove the old Office 365 domain

Finally use the following PowerShell script to remove the old Office 365 domain,

Input Parameters required: $olddomain - Office 365 domain name (old domain need to be removed)

step 1 :Get values for input parameters:

$olddomain =”olddomainname.com”

step 2 :Remove the old Office 365 domain:

Remove-MsolDomain -DomainName $olddomain -Force