Home 365 Domain Removal and Revert
Post
Cancel

365 Domain Removal and Revert

365 Domain Removal and Revert

Script to remove existing primary domain and revert to .onmicrosoft or other specified domain.

Specify Domains and Connect to 365

1
2
3
4
5
6
$olddomain ="docs.nels.one"
$Newdomain="nels.onmicrosoft.com"

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

Replace Existing UPN’s

1
2
3
4
5
6
7
$users=Get-MsolUser -domain $olddomain
$users | Foreach-Object{ 
$user=$_
$UserName =($user.UserPrincipalName -split "@")[0]
$UPN= $UserName+"@"+ $Newdomain 
Set-MsolUserPrincipalName -UserPrincipalName $user.UserPrincipalName -NewUserPrincipalName $UPN
}

Remove Address from Existing Mailboxes

1
2
3
4
5
6
7
8
9
10
11
Connect-ExchangeOnline
$Mailboxes = Get-Mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes) {

    $Mailbox.EmailAddresses | Where-Object { ($_ -like "*@docs.nels.one") } | 

    ForEach-Object {
        Set-Mailbox $Mailbox.Name -EmailAddresses @{remove = $_ }
        Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
    }
}
This post is licensed under CC BY 4.0 by the author.