Recursively get distribution list members
Hi, I've been asked to supply the membership of several of our large distribution lists. However, these lists are heavily nested, so I need to get membership recursively starting from the top level list. The old ONDL.EXE utility could do this. I was wondering if there was a powershell command that could accomplish the same thing. The get-distributiongroupmember cmdlet does not seem to have recursive abilities. If not, could someone help me with a script to do this? Thanks, Don
August 19th, 2007 9:21pm

I don't think the Get-DistributionGroupMember cmdlet will enumerate the membership recursively. However, you can write a simple script to do this. I don't know the exact syntax for this and don't have an E2K7 server around to test it, but you will need to enumerage the membership and then pipe that to another cmdlet to check the type of object, if it is a DL, you have to then enumerate that.
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2007 2:25pm

This is kind of long, but works: (watch line wraps) Save it as something like get-exgrouprecurse.ps1, and call it like this: $members = get-exgrouprecurse<group name> Then, $members will contain all the members full objects, and the group name they are actually in Code Snippet ##################################### # # # Script to retreive group members # # # # Karl Mitschke March 7 2008 # # # ##################################### ###################################### # heavily modified from recipe 7.3 # # in 'Active Directory Cookbook' # # by Robbie Allen # ###################################### #requires -pssnapin Microsoft.Exchange.Management.PowerShell.Admin param($group) $UnknownGroup = @{} function DisplayMembers($group) { $SubGroup = @{} $AllMembers = @() if(!$group) { $group = Read-Host "Enter the groups display name" } if ($group.Contains("'")) { $group = $group.Replace("'",'"') } if ($group -eq "/?") { Write-Host "Usage:" Write-Host "" Write-Host "get-exgrouprecurse -group <group name>" Write-Host "" Write-Host "or get-exgrouprecurse <group name>" Write-Host "Returns an object containing the group member, and the group name." break } $validate = Get-Group $group if ($validate.RecipientTypeDetails.ToString() -match "mail") { $searchGroup = Get-DistributionGroupMember $group if ($searchGroup) { foreach ($member in $searchGroup) { $membertype = $member.RecipientTypeDetails if($membertype -match "Group") { $samname = $member.SamAccountName.ToString() if ($SubGroup.ContainsKey($samname) -eq $true) { Write-Output "^ already seen group member (stopping to avoid loop)" } else { $SubGroup.Add($samname,$member.DisplayName.ToString()) } } else { if($member.PrimarySmtpAddress -and $member.RecipientTypeDetails -notcontains "group") { $obj = new-object psObject $obj | Add-Member -membertype noteproperty -name GroupMember -Value $member $obj | Add-Member -MemberType noteproperty -Name GroupName -Value $group $AllMembers += $obj } } } } else { $UnknownGroup.add($group,1) } if($SubGroup.Values.Count -gt 0) { foreach ($subGroup in $SubGroup.values) { DisplayMembers $subGroup } } if ($UnknownGroup.Keys.Count -gt 0) { foreach ($LostGroup in $UnknownGroup.keys) { $obj = new-object psObject $obj | Add-Member -membertype noteproperty -name GroupMember -Value "Cannot enumerate group" $obj | Add-Member -MemberType noteproperty -Name GroupName -Value $LostGroup $AllMembers += $obj } $UnknownGroup.Clear() } } else { Write-Output "$group does not appear to be mail enabled." } Write-Output $AllMembers } DisplayMembers $group
March 8th, 2008 12:34am

It seems this script only returns the first 1000 members. Would it be possible to define the -resultsize parameter to a specific value or unlimited? It would also be great if the OU or more parameters can be specified to be returned.
Free Windows Admin Tool Kit Click here and download it now
April 24th, 2008 5:03pm

This is great, however, what ifyou wanted to get members of numerous groups. I.E. lets say i need the members of group1, group2, group18, group25, etc and i don't want to have to do each one with a seperate command. Is this possible with that script?
August 6th, 2008 4:14pm

Hi, I have created a small, simple powershell script that does exactly that! You feed it an array of group names. It returns a tree view of the group members and subgroups it finds and finally returns a list of all unique group members and their email addresses. It's perfect for distribution lists. Give it a try: http://www.peetersonline.nl/index.php/powershell/listing-ad-group-members-recursively-with-powershell Hugo Peeters http://www.peetersonline.nl
Free Windows Admin Tool Kit Click here and download it now
November 18th, 2008 11:13am

I wrote a similar script that does not require the Quest cmdlets. It also includes dynamic distribution groups and their members in the result set.http://www.tinyint.com/index.php/2009/05/24/enumerate-distribution-group-members/
May 26th, 2009 10:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics