Powershell script to delete specific domain suffix from all mailbox smtp email addresses

Benard Mwanza 996 Reputation points
2021-10-02T06:10:02.3+00:00

I'm working on a PowerShell script to bulk remove any reference to a specific domain suffix in office 365 objects. The objects include office 365 groups, user email addresses, distribution lists, dynamic distribution lists, resources, shared mailboxes and user mailbox. The ultimate goal is to ensure that the domain does not have any reference in any of office 365 objects.

Have done dozen of research and i seem to find many script that only removes any email addresses associated to a specific domain pulled by this -EmailAddresses parameter when using get-mailbox and set-mailbox cmdlet. This works fine, however it only targets user mailboxes email addresses but not the other objects mentioned above.

The script is this one
` $mailboxes=get-mailbox -resultsize unlimited

foreach($mailbox in $mailboxes)

{

for($i=($mailbox.EmailAddresses.count)-1; $i -ge 0; $i--)

{
$address=$mailbox.EmailAddresses[$i]

     $addressString=$address.addressString  
 
     if($addressString -like "*$($_.samaccountname)@berniesoft.com*")  

     {  
        $mailbox.EmailAddresses.removeat($i)  
      }  

}  

$mailbox|set-mailbox -EmailAddresses $mailbox.EmailAddresses

}`

The requirement is to ensure that this script get-recipient | where {$_.EmailAddresses -match "berniesoft.com"} | fl Name, RecipientType, EmailAddresses returns nothing for that domain.

If someone has a similar script implementation, you can share here. Thank you.

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,151 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,344 questions
{count} votes

9 answers

Sort by: Most helpful
  1. Rich Matheisen 44,541 Reputation points
    2021-10-02T14:46:02.067+00:00

    Why not start with Get-Recipient and Where-Object (as above)? Pipe the results into a ForEach-Object where you can examine the properties of each object to determine its type (mailbox, mailuser, mailcontact, etc.) and then use the appropriate Set-* cmdlets (based on the type of object) to remove the addresses?

    0 comments No comments

  2. Limitless Technology 39,331 Reputation points
    2021-10-04T12:25:37.353+00:00

    Hello,

    maybe something like mentioned above to start with Get-Mailboxes:

    First get all mailboxes
    $mailboxes = get-mailbox;

    For each mailbox as mentioned

    foreach ($mailbox in $mailboxes) {
    
        $emailaddresses = $mailbox.emailaddresses;
    
    Each SMTP address found on each mailbox
        for ($i=0; $i -lt $emailaddresses.count; $i++) {
    
    Just change the domain name below to what you want to remove
            if ($emailaddresses[$i].smtpaddress -like "*domain.name.com*") {
    
    And  remove the unwanted email address
                $badaddress = $emailaddresses[$i];
                $emailaddresses = $emailaddresses - $badaddress;
                $mailbox | set-mailbox -emailaddresses $emailaddresses;
    
            }
    
        }
    
    }
    

    --If the reply is helpful, please Upvote and Accept as answer--


  3. Rich Matheisen 44,541 Reputation points
    2021-10-04T19:00:07.537+00:00

    Sticking to just updating one mailbox, this might work:

    $samaccountname = "JoeSchmoe"  
    $mbx = Get-Mailbox <name>  
    $update = $false  
    [array]$NewAddr = @()  
    $mbx.EmailAddresses |  
        ForEach-Object {  
            if ($_ -notlike "*$samaccountname@berniesoft.com*") {  
                # be sure that the leading "Type:" is part of the AddressString!!!!  
                $NewAddr += $_ # keep this address  
            }  
            else {  
                $update = $true                 # and don't keep the $samaccount@berniesoft.com one!  
            }  
        }  
    # You should check to be sure that there's still a PRIMARY SMTP address  
    # and if there isn't, provide for either inserting another address with a capitalized "SMTP:" or picking one address in $NewAddr and making IT the primary  
    if ($update) {  
        $mbx | Set-Mailbox -EmailAddresses $newAddr  
    }  
    

    I don't have an Exchange organization to work with any more, so I can't verify the above code! But I was an Exchange MVP for about 16 years before retiring so the code is at least roughly correct (unless retirement has severely eroded my memory!).


  4. Benard Mwanza 996 Reputation points
    2021-10-06T13:25:57.35+00:00

    Hi @Rich Matheisen

    Find attached the snipshot of errors that i'm getting on running the second script you provided.

    138187-snip2.png


  5. Benard Mwanza 996 Reputation points
    2021-10-06T13:33:43.43+00:00

    This is the logic i need to implement.

    CURRENT_VANITY.com is the undesired domain.

    For each Object {
    
    If the primary address is <CURRENT_VANITY.COM> {
    
                  If an <ON_MICROSOFT.COM> alias does not exist
    
                                 Add an alias with <ON_MICROSOFT.COM>
    
                  Set <ON.MICROSOFT.COM> address as primary
    
    }
    
    Delete all secondary addresses with <CURRENT_VANITY.COM> and only those ones
    
                  }
    
    0 comments No comments