Exchange Management Shell

suriyaehnop
suriyaehnop used Ask the Experts™
on
I would like to have a cmdlet which can remove the user from distributions list which he member of.

Let say, this userA is member of distri1,distri2,distri3...etct
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is the script for listing the groups the user belong to

http://myitforum.com/cs2/blogs/yli628/archive/2007/08/27/powershell-script-to-list-user-group-membership-in-active-directory.aspx 

Output of this pass it to Remove-DistributionGroupMember -Identity Groupnamefromabove  -Member 'DisplayNameofUser' or if you can pipe the output then Remove-DistributionGroupMember  -Member 'DisplayNameofUser'

Author

Commented:
Thank,

Is there any cmdlet in Exchange Management Shell that can query this

Commented:
You can use remove-distributiongroupmember

Remove-DistributionGroupMember -Identity "GROUPNAME" -Member "USER"
Solutions Architect
Commented:
The problem in what you want is that group members are actually a group properties and not a user one so it will be more than one cmdlet

if you want it in cmdlets

$grps = Get-DistributionGroup #this will return all distribution groups
$mgrps = $grps | where { (Get-DistributionGroupMember $_ | foreach {$_.name}) -eq "UserA"} #This will return all the groups this user is member off
$mgrps | Remove-DistributionGroupMember -Member UserA

If you prefer to have it as a script save the following as a RemoveFromAllDist.ps1

and run .\removefromalldist.ps1 "John Doe"

$u = $args[0]
foreach ($g in Get-DistributionGroup) { 
   foreach ($m in Get-DistributionGroupMember $g){
      if ($m.name -eq $u) {
         Remove-DistributionGroupMember $g -member $m -confirm:$false
         write-host $m.name " was removed from " $g
      }
   }
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial