M365 Domain Removal
Removing a domain from Office 365 using PowerShell involves several steps. Change the UserPrincipalName for all M365 Users, Change Email Addresses for all Microsoft 365 Mailboxes and Groups, Remove the old Microsoft 365 domain
Prerequisites
M365 Admin Access: You need to have the necessary admin permissions
Powershell : Install the Microsoft Azure Active Directory Module and Exchange Online Management Module for Windows PowerShell
Important Notes
Always ensure you have backups or have planned for any required changes before removing a domain.
Before running this script in a production environment, test it in a safe, non-production environment to avoid accidental data loss or service disruption.
# Output will be added to C:temp folder
Start-Transcript -Path "C:tempRemove-M365Domain.log" -Append
#Get values for input parameters
$olddomain =”joseph.com”
$Newdomain=“ludwig.com”
#Connect to MsolService
Import-Module MsOnline
$credential = get-credential
Connect-MsolService -Credential $credential
#Connect to Exchange Online:
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $credential -Authentication “Basic” –AllowRedirection
Import-PSSession $ExchangeSession
#Change the UserPrincipalName for all Microsoft 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
write-Host “The UserPrincipalName for all Microsoft 365 users has been successfully updated”
#Change Email Addresses for all Microsoft 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
}
write-Host “The email addresses for all Microsoft 365 mailboxes have been successfully updated”
#Change 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
}
write-Host “The email addresses for all Distribution Group have been successfully updated”
#Change Email Addresses for all Dynamic Distribution Groups
$Groups=Get-DynamicDistributionGroup
$Groups | Foreach-Object{
$group=$_
$groupname =($group.PrimarySmtpAddress -split “@”)[0]
$SMTP =“SMTP:”+$groupname+“@”+$Newdomain
$Emailaddress=$groupname+“@”+$Newdomain
$group |Set-DynamicDistributionGroup -EmailAddresses $SMTP -WindowsEmailAddress $Emailaddress -MicrosoftOnlineServicesID $Emailaddress
}
write-Host “The email addresses for all Dynamic Distribution Group have been successfully updated”
#Change Email Addresses for all M365 Groups
$Groups=Get-UnifiedGroup
$Groups | Foreach-Object{
$group=$_
$groupname =($group.PrimarySmtpAddress -split “@”)[0]
$SMTP =“SMTP:”+$groupname+“@”+$Newdomain
$Emailaddress=$groupname+“@”+$Newdomain
$group |Set-UnifiedGroup -EmailAddresses $SMTP -WindowsEmailAddress $Emailaddress -MicrosoftOnlineServicesID $Emailaddress
}
write-Host “The email addresses for all M365 Group have been successfully updated”
#Remove the old Microsoft 365 domain
Remove-MsolDomain -DomainName $olddomain -Force
write-Host “The old Microsoft 365 domain has been successfully removed.”
Stop-Transcript