Share via


- Bulk removal of Distribution group email addresses

Question

Monday, August 12, 2019 3:41 PM

Hi all,

the previous exchange admin neglected to exclude old domains from the previous policy and now every single distribution list, contact and mailbox user has got 20 or so individual smtp addresses added to the object.  I have since fixed the policy and created new ones for mailboxes, distribution groups etc.

I've been trying to come up with a good script to fix this, either by using the Set-Distributiongroup command or Set-ADgroup.

We are currently running as a Hybrid configuration with a Exchange 2010 on premise sever for management so ideally we need to run any scripts against the local environment.  Of course I cannot just set a new policy as policy overwrites do not take away any email addresses.  Hopefully somebody has a script that can be easily amended to do the job, there's around 7000 objects that need amending, contacts and remote mailboxes are easy enough, but amending 4000 distribution lists en mass to remove a proxy address?

Thanks

All replies (9)

Wednesday, August 14, 2019 8:45 AM âś…Answered

Hi,

I know about this issue now, the email you want to delete aren't in the format of {"alias" + "domain name"}, some of them are in the format of {"firstName.LastName" + "domain name"}.

We can use regular expressions to find the correct address(This script will find the full email address with domain name and ignore the prefix of this email address):

$Groups = Get-DistributionGroup -Resultsize unlimited | where {$_.EmailAddresses -like "*domain.com*"}

foreach($Group in $groups){  
    
    $temp = $Group.Name
    $temp2 = ((Get-DistributionGroup $temp).EmailAddresses -like "*@domain.com") -replace "\w*\:" #Using domain name to find the full address

    Set-DistributionGroup $temp -EmailAddresses @{remove=$temp2}
}

Here is an demo for it:

Regards,

Kyle Xu

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Monday, August 12, 2019 3:54 PM

Set-DistributionGroup with the -EmailAddresses parameter is all you need, you simply need to prepare the list of addresses to include. Or you can just take the already existing list, then remove any addresses associated with domains you don't care about. I have some samples on how to do this here: https://www.michev.info/Blog/Post/2011/bulk-replacing-proxy-addresses-based-on-a-pattern


Tuesday, August 13, 2019 7:17 AM

Hi,

Do you want to remove all "stmp" email address and only keep the "SMTP" email address?

If so, you can use command below:

Set-DistributionGroup group -EmailAddressPolicyEnabled $false

Set-DistributionGroup group -EmailAddresses [email protected]

If you just want to remove a specific email address from this group, you can use command below:

Set-DistributionGroup group -EmailAddresses @{remove="[email protected]"}

Above are two scenarios for a specific group, if you want to modify for all group, you can add them into a script.

Regards,

Kyle Xu

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Tuesday, August 13, 2019 8:45 AM

Thank you for the swift replies all,

we need to take away roughly 20 smtp addresses from every distribution group (2400), leaving only our primary domain and O365 tenant addresses.

Ideally I'd want to create a variable that finds all groups with the additional domains and then perhaps does a for each and removes the additional addresses by turn.  Unless there is a way of just deleting all addresses other than the tenant and the primary?

My Powershell isn't amazing, but here's what I've got so far.  Ill have a good read through your suggestions.

Thanks

Robbie

$groups=Get-DistributionGroup -resultsize unlimited
  
 foreach($group in $groups) 
 {
   where{$_.emailaddress -like 'domain.com'}| foreach{
  
 Set-DistributionGroup -identity $group -EmailAddresses @{remove=$_}
 Write-Host "address removed from $group"
  
    }
  
 }

Tuesday, August 13, 2019 9:18 AM

Hi,

This script may be easier to understand:

$Groups = Get-DistributionGroup -Resultsize unlimited | where {$_.EmailAddresses -like "*domain.com*"} #Select all distributions which containes "domain.com"

foreach($Group in $Groups){  
    
    $temp = $Group.Name #Select group name
    $temp2 = $temp+"@domain.com" #Pass value "[email protected]" to temp2

    Set-DistributionGroup $temp -EmailAddresses @{remove=$temp2} #Remove this email address
}

Regards,

Kyle Xu

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Tuesday, August 13, 2019 9:48 AM

Thanks Kyle,

that makes more sense, however when testing on one mailbox i'm presented with the following error:

WARNING: The command completed successfully but no settings of '*****.corpGroups/Distribution Lists/*********' have been modified.


Wednesday, August 14, 2019 8:09 AM

Could you description about how do you testing on one mailbox?

I guess this mailbox doesn't have such an email address, so this script run successfully without make any change to this mailbox.

Regards,

Kyle Xu

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].


Wednesday, August 14, 2019 8:17 AM

Hi Kyle,

thanks for your continued support on this.  I've figued out what the issu is however it's a bit of a headcase scenario:

The previous admin has also used multiple email policies applied to all recipient types, so for example:

Policy 1 was [email protected]  "[email protected]"

Policy 2 was [email protected] "[email protected]"

Policy 3 was [email protected] "[email protected]"

so now we can see the issue, where pretty much every object has different email addresses based on old policies, the script ive used below using your code finds all groups with the email address like the domain i want to remove, but uses alias name and display name to remove.  the only issues I have now is removing those addresses that are out of policy, but where the domain to remove still exists (eg [email protected]).

Could it be possible to do something clever in regex to not care about the first part of the email address and only use words after the "@" symbol?

Thanks

robbie

$Groups = Get-DistributionGroup -Resultsize unlimited | where {$_.EmailAddresses -like "*domain.com*"} #Select all distributions which containes "domain.com"

foreach($Group in $Groups)

{  
    $alias = $Group.Alias #Select group alias
    $name = $group.name #Select group name
    $displayname =  $group.DisplayName #select display name

    $emailtoremove = $alias+"@domain.com"
    $emailtoremove2 = $name+"@domain.com"
    $emailtoremove3 = $displayname+"@domain.com"

    
    #get rid of email address on the domain we want to use trying alias, name and display name
             
      Set-DistributionGroup $alias -EmailAddresses @{remove=$emailtoremove} #Remove this email address
      Set-DistributionGroup $alias -EmailAddresses @{remove=$emailtoremove2} #Remove this email address
      Set-DistributionGroup $alias -EmailAddresses @{remove=$emailtoremove3} #Remove this email address

     
      Write-Host $group $emailtoremove $emailtoremove2 $emailtoremove3 "removed"                                
    }
  

 


Wednesday, August 14, 2019 9:43 AM

Thanks again,

I added in a Loop to grab to go through each smtp address in the emailaddresses field and find the onee I want and throw it in to another variable.

Working now:)

Thanks!