Not sure I understand the question here? The code should work for both "regular" DGs and mail-enabled security ones (fixed membership).
Here's a reference script I did a while back that should output membership of nested groups: https://github.com/michevnew/PowerShell/blob/master/DG_members_recursive.ps1
And the accompanying article with more details: https://practical365.com/how-to-inventory-membership-of-exchange-groups-recursively/
Nested DLs Nightmare. Please HELP
Can someone help me add to this function to where I can feed 300+ DLs into it to get the number of members nested and un-nested in each DL. It works when hardcoded in the $group variable. When it runs it displays all the members and the $members.count gives me the number of members in the DL. I have to convert 300+ mail enabled security groups to DL. Before working with them I want to work with the ones with the least number of members first.
$group = "DL-Name"
$members = New-Object System.Collections.ArrayList
Create the Function
function getMembership($group) {
$searchGroup = Get-DistributionGroupMember $group -ResultSize Unlimited
foreach ($member in $searchGroup) {
if ($member.RecipientTypeDetails-match "Group" -and $member.DisplayName -ne "") {
getMembership($member.DisplayName)
}
else {
if ($member.DisplayName -ne "") {
if (! $members.Contains($member.DisplayName) ) {
$members.Add($member.DisplayName) >$null
}
}
}
}
}
Run the function
getMembership($group)
Output results to screen
##$members.GetEnumerator() | sort-object > .\all_members_mailing_list.csv
$members | Select @{N="UserName";E={$_}}
$members.count
2 answers
Sort by: Most helpful
-
Vasil Michev 104K Reputation points MVP
2021-06-16T08:09:50.18+00:00 -
KyleXu-MSFT 26,256 Reputation points
2021-06-17T09:13:33.697+00:00 Why not add a loop to your script directly, such as:
function getMembership($group) { $searchGroup = Get-DistributionGroupMember $group -ResultSize Unlimited foreach ($member in $searchGroup) { if ($member.RecipientTypeDetails-match "Group" -and $member.DisplayName -ne "") { getMembership($member.DisplayName) } else { if ($member.DisplayName -ne "") { if (! $members.Contains($member.DisplayName) ) { $members.Add($member.DisplayName) >$null } } } } } $allGroups = Get-DistributionGroup -ResultSize Unlimited foreach ($allgroup in $allGroups) { $members = New-Object System.Collections.ArrayList Write-Host ("`nInformation about "+$allgroup) getMembership($allgroup ) $members | Select @{N="UserName";E={$_}} Write-Host ("Member Count "+$members.count) }
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.