Forum Discussion
Powershell script to get all group membership(UG or DL) and remove that user only to that group
You can combine the two scripts as follows. Here you search the groups where the user is member and then removes the user from those groups.
$email= read-host -prompt "user@domain.com"
$Mailbox=get-Mailbox $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter | %{Remove-DistributionGroupMember -identity $_.name -Member $email -confirm:$false}
Get-UnifiedGroup -ResultSize Unlimited -Filter $Filter | %{Remove-UnifiedGroupLinks -Identity $_.name -LinkType Members -Links $email -Confirm:$false}
Hi Nestori,
Tried this but seem it doesn't work, I like this code as it is simple.
Nestori Syynimaa wrote:You can combine the two scripts as follows. Here you search the groups where the user is member and then removes the user from those groups.
$email= read-host -prompt "user@domain.com"
$Mailbox=get-Mailbox $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter | %{Remove-DistributionGroupMember -identity $_.name -Member $email -confirm:$false}
Get-UnifiedGroup -ResultSize Unlimited -Filter $Filter | %{Remove-UnifiedGroupLinks -Identity $_.name -LinkType Members -Links $email -Confirm:$false}
Best Regards
Mark Diaz
- Jul 17, 2018
Hi Mark,
Did you got some error message? You need to connect to Exchange Online before running the script (see for example http://o365blog.com/powershell)
- Mark Louie DiazJul 17, 2018Copper Contributor
Hi Nestori,
It is now working Many Thanks !
Nestori Syynimaa wrote:Hi Mark,
Did you got some error message? You need to connect to Exchange Online before running the script (see for example http://o365blog.com/powershell)
Best Regards,
Mark Diaz