How to bulk remove contacts from distribution group and also from exchange online?

rntongoo 1 Reputation point
2021-11-19T20:31:28.9+00:00

Hi,

I was able to bulk insert from a CSV into Exchange Online using this script:
Import-Csv .\ExternalContacts.csv|%{New-MailContact -Name $.ExternalEmailAddress -DisplayName $.Name -ExternalEmailAddress $.ExternalEmailAddress -FirstName $.FirstName -LastName $_.LastName}

I was also able to bulk insert contacts into a distribution group using this script:
Import-CSV .\ExternalContacts.csv | % { Add-DistributionGroupMember DG -Member $_.ExternalEmailAddress }

I need code to bulk remove from an exchange group:

Then Bulk Delete from exchange online:

Would you please help me with this task?
Thank you

Exchange | Exchange Server | Management
Windows for business | Windows Server | User experience | PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. KyleXu-MSFT 26,396 Reputation points
    2021-11-22T07:43:01.083+00:00

    @rntongoo

    Use command below to remove Contact from group:

    Import-CSV .\ExternalContacts.csv | % { Remove-DistributionGroupMember DG -Member $_.ExternalEmailAddress -Confirm:$false}  
    

    Use command below to remove Contact from Exchange Online:

    Import-Csv .\ExternalContacts.csv|%{Remove-MailContact $_.ExternalEmailAddress -Confirm:$false}  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

  2. Limitless Technology 39,926 Reputation points
    2021-11-22T21:12:12.04+00:00

    Hello @rntongoo

    To discover users in group:

    Get-ADUser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net" -properties * | ft displayname, emailaddress  
    

    To remove malboxes (format: Identity, EmailAddress):

    Import-Csv "F:\Users.csv"  | foreach { Remove-Mailbox -Identity $_.Identity }  
    

    Hope this helps with your query,

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.