Bulk change parameters (domain\alias)

October 20, 2018 | by iaur | posted as Adminstration, Microsoft 365


  • You will need to remove the domain from a specific tenant but unable to do so since it is currently being use


This example is using faceresionem.onmicrosoft.com as the initial domain (MODRD) and will replace their vanity domain to MODRD (domain.onmicrosoft.com)


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

*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-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 O365 group

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”) }

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

Bulk change samaccountname\alias

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

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