Script to remove a user from all distribution groups managed in the cloud

The purpose of this PowerShell script is to remove a user from all distribution groups. The script is interactive, it will show the list of groups first, then you have an option to process your request.

While the same can be done using Exchange Admin or Microsoft 365 Admin Center, it is much faster using PowerShell.

Note, the script only processes those distribution groups that are managed in the cloud. If you have some groups synced with Active Directory, those should be processed separately.

$email = Read-Host "Please provide a user's email address to remove from all distribution groups"

$mailbox = Get-Mailbox -Identity $email

$DN=$mailbox.DistinguishedName

$Filter = "Members -like ""$DN"""

$DistributionGroupsList = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter

Write-host `n
Write-host "Listing all Distribution Groups:"
Write-host `n
$DistributionGroupsList | ft

$answer = Read-Host "Would you like to proceed and remove $email from all distribution groups ( y / n )?" 

While ("y","n" -notcontains $answer) {
	$answer = Read-Host "Would you like to proceed and remove $email from all distribution groups ( y / n )?"
	}

If ($answer -eq 'y') {

	ForEach ($item in $DistributionGroupsList) {
		Remove-DistributionGroupMember -Identity $item.DisplayName –Member $email –BypassSecurityGroupManagerCheck -Confirm:$false
	}
	
	Write-host `n
	Write-host "Successfully removed"

	Remove-Variable * -ErrorAction SilentlyContinue
	}

Else

	{
	Remove-Variable * -ErrorAction SilentlyContinue
	}

This Post Has 12 Comments

  1. MT

    Thank you so much for this!!!! I’ve been looking for a script or guidance on how to write one that works, stumbled across your script. Thank you!

    1. Pavel Bludov

      You are very welcome!

  2. Alex B.

    Excellent script! if I am on an Exchange Hybrid environment do you know if this will then work on all distro lists since with Hybrid we are synchronized with the cloud? If you could let me know your thoughts that would be appreciated. Thank you.

  3. Ras

    The Script worked for me. Just a question can we add lines to remove the o365 groups(all including Teams enabled) as well

    1. Pavel Bludov

      Hi Ras,
      Yes, this is a very old script from back then. If you have access to Power Automate and Graph API, I would recommend you switch to it as it makes automation much easier.
      To be honest, this current script needs a complete “redo”.

        1. Paul Bludov

          Thanks Loryan,
          You are correct on that. We try to use as many dynamic groups as possible (which get updated once a person leaves the company). The rest is taken care of by the Graph API workflows. Those groups we cannot automatically process, a workflow puts into an automated report and creates a ticket for the Help Desk Team.

  4. Armstrong Gbewonyo

    Great Script, saves me a lot of time.
    Thank you

    1. Paul Bludov

      Thanks Armstrong!
      It looks I have to update my early 2019 script given that Google started returning it in search results.

  5. Sang L

    This script does what we needed it to do! Thanks so much Alex! I could only imagine what the updated version looks like. You’re a true lifesaver!

  6. Nirmal

    Hi Paul,

    Not sure if I am doing anything wrong, I am trying to use this script and its giving me below error, can you help

    Remove-DistributionGroupMember : A positional parameter cannot be found that accepts argument ‘â€Member
    nikhila.sontaya@olympus.com â€BypassSecurityGroupManagerCheck’.
    At C:\Scripts\dl.ps1:25 char:9
    + Remove-DistributionGroupMember -Identity $item.DisplayName †…
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Remove-DistributionGroupMember], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-DistributionGroupMember

Leave a Reply