Bulk change default domain objects in your organization

Scenario

An admin was tasked to create a new Microsoft 365 tenancy and migrate user. The admin then planned and execute the removal of the domain from the Admin centers however, for some reason the admin cannot do so since it is being utilized.

The solution was to revert all users\objects to use the default domain and to use bulk scripting.

Steps

In this article, we will be using faceresionem.onmicrosoft.com as the initial domain (MODRD) and will replace their custom domain to the MODRD.

It is important to review each cmdlets and connect to specific remote service before executing the cmdlets. Please see this article for additional information.

MSOL

This is to replace domain for all active user except for an alias that starts with “admin” (admin is usually utilized as a break-glass or service account).

Get-MsolUser -all | Where { -Not $_.UserPrincipalName.ToLower().StartsWith(admin@) } | ForEach { Set-MsolUserPrincipalName -ObjectId $_.ObjectId -NewUserPrincipalName ($_.UserPrincipalName.Split(@)[0] + @faceresionem.onmicrosoft.com) }

EXO

This is to replace domain for all O365 group.

Get-UnifiedGroup | ForEach { Set-UnifiedGroup -Identity $_.PrimarySmtpAddress -PrimarySmtpAddress ($_.PrimarySmtpAddress.Split(@)[0] + @faceresionem.onmicrosoft.com) }

Get-UnifiedGroup | ForEach { Set-UnifiedGroup -Identity $_.PrimarySmtpAddress -EmailAddresses ($_.PrimarySmtpAddress.Split(@)[0] + @faceresionem.onmicrosoft.com) }

This is to replace domain for all distro\security groups.

Get-Mailbox | ForEach { Set-Mailbox -Identity $_.PrimarySmtpAddress -PrimarySmtpAddress ($_.PrimarySmtpAddress.Split(@)[0] + @faceresionem.onmicrosoft.com) }

Get-Mailbox | ForEach { Set-Mailbox -Identity $_.PrimarySmtpAddress -EmailAddresses ($_.PrimarySmtpAddress.Split(@)[0] + @faceresionem.onmicrosoft.com) }

A miscellaneous,

Replaces all emailAddress with “carlotrial” then replaces it with “CARLOTRIAL” samaccount/alias

get-mailbox | where {$_.primarysmtpaddress -match carlotrial.} | select alias, emailaddresses | foreach { Set-mailbox -Identity $_.alias -EmailAddresses ($_.emailaddresses -replace Carlotrial, CARLOTRIAL)}

 

References