Exchange Online – Remove Domain From Multiple Mailboxes

In this blog post, I will show you how to remove an email domain from all users mailbox using Exchange Online PowerShell.

Sometimes when companies split or being sold, there is a need to do a clean up in the directory.

In my case, I need to remove an email domain from all my Exchange Online users.

Connect to Exchange Online PowerShell

The first step in this process is to connect to Exchange Online using PowerShell and the best way to do it is using Cloud Shell.

In my previous post, I showed how to use it.

Once connected, I will use the following PowerShell script to delete all users.

I am using a text file with the list users (email address) I have exported from the previous blog post.

, copy the name to a text file called users.txt use with the script.

The script will loop over the list and remove the domain from all the users.

$Mailboxes = Get-Content .users.txt| Get-Mailbox
$Mailboxes | foreach { for ($i=0;$i -lt $_.EmailAddresses.Count; $i++)
{
$address = $_.EmailAddresses[$i]
if ($address.IsPrimaryAddress -eq $false -and $address.SmtpAddress -like "*ntweekly.com" )
{
Write-host($address.AddressString.ToString() )
$_.EmailAddresses.RemoveAt($i)
$i--
}
}
Set-Mailbox -Identity $_.Identity -EmailAddresses $_.EmailAddresses
}

Posted

in

by