The following script can be used to remove old email addresses (based on a particular domain name) from all mailboxes.
IE; if you wanted to remove the SMTP address “@tms.net.au” from all users, you can use the following script in an Exchange Management Console:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Get all mailboxes $mailboxes = get-mailbox; # Loop through each mailbox foreach ($mailbox in $mailboxes) { $emailaddresses = $mailbox.emailaddresses; #Loop through each SMTP address found on each mailbox for ($i=0; $i -lt $emailaddresses.count; $i++) { # Change the domain name below to what you want to remove if ($emailaddresses[$i].smtpaddress -like "*tms.net.au*") { # Remove the unwanted email address $badaddress = $emailaddresses[$i]; $emailaddresses = $emailaddresses - $badaddress; $mailbox | set-mailbox -emailaddresses $emailaddresses; } } } |
It’s important to remember, when changing the domain name in the above script, that you must leave at least one wildcard character (*) in the query, otherwise it will fail.
Nice script, thanks for sharing
Thank you for sharing this. I recently had to prep our accounts for O365 and when the domain was initially created they set our email policy to include the @.local alias to the accounts
All the other examples I found seemed to rely on dumping the bad domains into a CSV or manually writing down all the accounts for the suffix you want to remove. This one was the only I found that truly removed the domain suffix from all my mailboxes programmatically
Pingback: Remove Domain Suffix for All Users | ATS Cloud
Excellent article.. thanks for sharing.
copied and pasted, changed the domain to remove and nothing happens.
Running PowerShell as Admin solves this problem.
I got this warning:
WARNING: By default, only the first 1000 items are returned.
How can we add the unlimited parameter in there?
You need to use the -resultsize parameter in the get-mailbox command, with the “unlimited” value.
EG:
$mailboxes = get-mailbox -resultsize unlimited;
You saved me an absolute headache. Thank you so much!
Hi,
I`m looking for a script which does the other way around. Meaning, from the list of smtp addresses should everything be deleted, *except* the ones which I mention in the if-loop. I tried with this version, but the output would also delete the primary mail address. Any idea would be highly appreciated?
# Loop through each mailbox
foreach ($mailbox in $mailboxes) {
$emailaddresses = $mailbox.emailaddresses;
#Loop through each SMTP address found on each mailbox
for ($i=0; $i -lt $emailaddresses.count; $i++) {
# Change the domain name below to what you want to remove
if (($emailaddresses[$i].smtpaddress -notlike “*O5*” ) -or ($emailaddresses[$i].smtpaddress -notlike “Administrative”) -or ($emailaddresses[$i].smtpaddress -notmatch “phone”))
{
# Remove the unwanted email address
$badaddress = $emailaddresses[$i];
$emailaddresses = $emailaddresses – $badaddress;
$mailbox | set-mailbox -emailaddresses $emailaddresses -WhatIf;
}
}
}
I think I know where you’ve gone wrong here. Change the -or’s to -and’s in the if statement:
if (($emailaddresses[$i].smtpaddress -notlike "*O5*" ) -and ($emailaddresses[$i].smtpaddress -notlike "Administrative") -and ($emailaddresses[$i].smtpaddress -notmatch "phone"))
Thank you for the script Kamal. I would like to use it to remove the primary email domain in preparation for removing that domain from the O365 tenancy. Will it fail if it’s the primary address and/or is there an option to specify what the new primary mail domain should be?
I haven’t tried it, but i would expect it to fail if you tried to remove the Primary SMTP address.
As part of the script, just after the initial foreach, I would set the new primary SMPT address with something like (making sure you have the correct variables set first):
set-mailbox $samaccountname -primarysmtpaddress $newprimarysmtpaddress
This is great! It works perfectly.
Thank you very much!!!!!