How do I delete a specific domain suffix from all mailbox SMTP email addresses?

orange

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:

 

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.

orange

14 Comments How do I delete a specific domain suffix from all mailbox SMTP email addresses?

  1. Todd

    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

    Reply
  2. Pingback: Remove Domain Suffix for All Users | ATS Cloud

  3. ozz

    I got this warning:
    WARNING: By default, only the first 1000 items are returned.
    How can we add the unlimited parameter in there?

    Reply
    1. Kamal

      You need to use the -resultsize parameter in the get-mailbox command, with the “unlimited” value.
      EG:
      $mailboxes = get-mailbox -resultsize unlimited;

      Reply
  4. Thomas

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

    }

    }

    Reply
    1. Kamal

      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"))

      Reply
  5. Michelle

    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?

    Reply
    1. Kamal

      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

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *