Connect-ExchangeOnline
# Define the domain to remove
$domainToRemove = "domain.com.au"
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited
foreach ($mailbox in $mailboxes) {
# Get current email addresses
$currentAddresses = $mailbox.EmailAddresses
# Filter out addresses with the specified domain
$updatedAddresses = $currentAddresses | Where-Object { $_ -notlike "*@$domainToRemove" }
# Update the mailbox if any addresses were removed
if ($currentAddresses.Count -ne $updatedAddresses.Count) {
Write-Host "Updating mailbox for: $($mailbox.PrimarySmtpAddress)"
#Fixes WARNING: Proxy address is used as WindowsLiveId. So it can't be removed from list of email addresses. To remove it, first change
the WindowsLiveId.
Set-Mailbox -Identity $mailbox.PrimarySmtpAddress -MicrosoftOnlineServicesID $mailbox.PrimarySmtpAddress
Set-Mailbox -Identity $mailbox.Identity -EmailAddresses $updatedAddresses
} else {
# Write-Host "No changes needed for: $($mailbox.PrimarySmtpAddress)"
}
}
Write-Host "Completed processing all users."