How to remove members without mailbox from distribution group in bulk?

Shawn Deng 46 Reputation points
2022-04-16T06:44:59.953+00:00

DG: Pure Cloud
Members in DG: some users don't have M3/M5 anymore. We'd like to remove these users without mailbox in a specific DG.
Is there a way to achieve it?

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,178 questions
0 comments No comments
{count} votes

Accepted answer
  1. KyleXu-MSFT 26,396 Reputation points
    2022-04-18T07:48:30.68+00:00

    @gaiusg

    You could have a try with this script (modify OnlineList1 to "Pure Cloud"):

    $users = Get-DistributionGroupMember OnlineList1 | where {$_.RecipientType -ne "UserMailbox"}  
      
    foreach($user in $users){  
        Remove-DistributionGroupMember -Identity OnlineList1 -Member $user.Name -Confirm:$False  
    }  
    

    193803-qa-kyle-15-45-52.png


    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.



1 additional answer

Sort by: Most helpful
  1. Michel de Rooij 1,546 Reputation points MVP
    2022-04-16T08:51:49.16+00:00
    $DG='YourDG'
    Get-DistributionGroupMember -Identity $DG | Where {$_.RecipientType -inotlike '*Mailbox'} | Remove-DistributionGroupMember -Identity $DG -Confirm:$False
    

    This will:

    • Get all members of the distribution group
    • Filter all members that are not of the recipient type ending in Mailbox, e.g. MailUser etc.
    • Remove members passing that filter from your distribution group

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.