Announcement: Need a free practice exam or an interactive mocks material for a Microsoft certification? Click here to use the MS Forms I've created previously.

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 as the initial domain (MODRD) and will replace their vanity domain to MODRD (


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

*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] + “”) }
Get-UnifiedGroup | ForEach { Set-UnifiedGroup -Identity $_.PrimarySmtpAddress -EmailAddresses ($_.PrimarySmtpAddress.Split(“@”)[0] + “”) }

*This is to replace domain for all O365 group

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

*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

Feedback or Help?

Should you have one, click here

Feeling grateful?

Boring Ads. Pardon me :)