Remove Domain Name from Office 365 Tenant

Hello!

In this article I will show you PowerShell Script that help to you delete domain name from all Office365 object.

 

#EXCH ONLINE
Set-ExecutionPolicy RemoteSigned -Force
$username = “”
$password = ” | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
## AZURE AD
Connect-MsolService -Credential $cred

####—DOMAIN NAMES—####

$olddomain =””
$Newdomain=””

Find all Users, Distrubution Groups and Shared Mail BOX with DomainName

Get-MsolUser -DomainName $olddomain | fl UserPrincipalName
get-recipient | where {$_.EmailAddresses -match “$olddomain”} | fl Name, RecipientType, EmailAddresses

Get-MsolUser -DomainName $olddomain

#Groups
Get-DistributionGroup | where {$_.EmailAddresses -like “*$olddomain”}
#UsersMailBox
Get-Mailbox -ResultSize unlimited | ? {$_.isShared -like $false -and $_.PrimarySmtpAddress -like “*$olddomain”}

#Shared MailBox

Get-Mailbox -ResultSize unlimited | ? {$_.isShared -like $true -and $_.PrimarySmtpAddress -like “*$olddomain”}

 

Start delete

#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
}

#Usermailbox and Shared
$Users = get-mailbox | where {$_.PrimarySmtpAddress -like “*$olddomain”}
$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
}

#Groups
$Groups = Get-DistributionGroup | where {$_.EmailAddresses -like “*$olddomain”}
$Groups | Foreach-Object{
$group=$_
$groupname =($group.PrimarySmtpAddress -split “@”)[0]
$SMTP =”SMTP:”+$groupname+”@”+$Newdomain
$Emailaddress=$groupname+”@”+$Newdomain
$group | Set-DistributionGroup -EmailAddresses $SMTP -WindowsEmailAddress $Emailaddress
}

$Records = Get-mailbox -ResultSize Unlimited| where {$_.emailaddresses -like “smtp:*@$olddomain”} | Select-Object DisplayName,@{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -like “smtp:*$olddomain”}}}
foreach ($record in $Records)
{
write-host “Removing Alias” $record.EmailAddresses “for” $record.DisplayName
Set-Mailbox $record.DisplayName -EmailAddresses @{Remove=$record.EmailAddresses}
}

$RecordsSIP = Get-mailbox -ResultSize Unlimited| where {$_.emailaddresses -like “sip:*@$olddomain”} | Select-Object DisplayName,@{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -like “sip:*$olddomain”}}}

foreach ($recordSIP in $RecordsSIP)
{
write-host “Removing SIP” $recordSIP.EmailAddresses “for” $recordSIP.DisplayName
Set-Mailbox $recordSIP.DisplayName -EmailAddresses @{Remove=$recordSIP.EmailAddresses}
}

Remove-MsolDomain -DomainName $olddomain -Force

One thought on “Remove Domain Name from Office 365 Tenant

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s