Share via


Get-DistributionGroupMember Recursive Functionality.

Question

Tuesday, October 25, 2011 1:50 PM

I’m looking to create a script that can use a list of groups to go through and get me the members of each group.

Example

$group = Get-DistributionGroupMember –identity “Group Name” | Select DisplayName

Now the content of $group will be a list of Distribution Groups, I need to use this as the input to create a CSV file of all the members of each of the groups in $group.

Can someone help me with this?

Ideally if Get-DistributionGroupMember had a recursive function I wouldn’t need to figure out a way to do this. I have seen a couple enumeration scripts around, but I’m not getting exactly the results I need. Any help would be appreciated.

 

Paul Arbogast

All replies (10)

Tuesday, October 25, 2011 2:58 PM

This is what I'm trying to do, but this doesnt work.

$group = Get-DistributionGroupMember –identity “DistributionListName” | Select DisplayName

$group | ForEach-Object {Get-DistributionGroupMember -identity $_ | Select DisplayName,PrimarySMTPAddress}

Paul Arbogast


Tuesday, October 25, 2011 5:13 PM

Thanks Richard, I'll see what I can do to make it work in my situation, I think I am getting closerPaul Arbogast


Tuesday, August 20, 2013 3:19 PM | 2 votes

Short and sweet recursive search filter >

filter get_member_recurse {
if($_.RecipientType -eq "MailUniversalDistributionGroup") {
Get-DistributionGroupMember $_.Name | get_member_recurse
} else {
$_
}
}

To use: $getmembers = get-distributiongroupmember "mygroup" | get_member_recurse


Monday, August 26, 2013 3:05 AM

I wrote a script some time ago to do this:

http://unlockpowershell.wordpress.com/2009/11/24/get-distributiongroupmember-recursively/

Karl

When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})


Monday, February 22, 2016 7:42 PM

I stumbled on this thread while working on the same problem. Here's a potentially simpler way to do it:

$stuff = get-distributiongroupmember [email protected]

$stuff_people = @()
$stuff_people += $stuff | where {$_.recipienttype -eq "UserMailbox"}
$stuff = $stuff | where {$_.recipienttype -ne "UserMailbox"}
    do
        {
        $thislist = $stuff | where {$_.recipienttype -ne "UserMailbox"}
                    
        foreach ($item in $thislist)
            {
            $stuff += Get-DistributionGroupMember -identity $item.primarysmtpaddress.tostring()

            }
        $stuff_people += $stuff | where {$_.recipienttype -eq "UserMailbox"}
        $stuff = $stuff | where {$_.recipienttype -ne "UserMailbox"}
        write-host "loop"
        }

    until (($stuff | where {$_.recipienttype -eq "UserMailbox"}).count -lt 1)

It probably could be cleaned up slightly as I know there are a couple redundant lines in there but that should do the trick.


Friday, March 25, 2016 8:11 PM | 2 votes

This is the function I am using which works in my testing.

function Get-DistributionGroupMemberRecursive ($GroupIdentity) {
    $member_list = Get-DistributionGroupMember -Identity $GroupIdentity
    foreach ($member in $member_list) {
        if ($member.RecipientType -like '*Group*') {
            Get-DistributionGroupMemberRecursive -GroupIdentity $member.Identity
        } else {
            $member
        }
    }
}

$group = Get-DistributionGroup "GroupName"
Get-DistributionGroupMemberRecursive -GroupIdentity $group.Identity

Tuesday, May 3, 2016 5:16 AM | 3 votes

Use Activedirectory and Exchange PowerShell 

$members = get-ADgroupmember groupsammaccountname -Recursive

$members |  foreach {Get-Mailbox $_.distinguishedName} | select displayname,primarysmtpaddress | export-csv allthmembers.csv -Append

Done


Tuesday, May 24, 2016 12:35 AM

Excellent.  Thank you.


Friday, August 19, 2016 8:09 PM

No need to touch exchange, this is what I use...

Get-ADGroupMember -Identity "some group" -Recursive | Get-ADUser -pr Name,mail | Select Name,mail | sort name | convertTo-HTML | Out-File C:\path\file.html

Wednesday, October 24, 2018 7:50 PM

Perfect, short and sweet!