list all group names from a nested group

Austin Sundar 436 Reputation points
2020-12-02T09:55:57.86+00:00

I am trying to get all the group names from a nested group. Could someone help me to get all group names from a nested group?
I tried the below scripts.
This will show groups but not showing the group name inside each groups.
Get-ADGroupMember -Identity "GroupName" | where-object {$_.objectClass -eq 'group'} | select name, distinguishedName

This seems not working for me
Get-ADGroupMember -Identity top -Recursive | `
Get-ADUser -Filter {Enabled -eq $true} | ForEach-Object {
New-Object PSObject -Property @{
UserName = $.DisplayName
AccountName = $
.SamAccountName
Groups = ($_.memberof | Get-ADGroup | (Select-Object -ExpandProperty Name) -join ","}
} | Select-Object UserName,AccountName,Groups

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Exchange | Exchange Server | Management
Exchange | Exchange Server | Management
The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Lucas Liu-MSFT 6,191 Reputation points
    2020-12-03T06:54:57.427+00:00

    Hi @Austin Sundar ,
    Please run the following script to view the all group names from a nested group, "Group Name" means the group you want to view:

    Function Get-ADNestedGroups {  
        param($Members)  
      
        foreach ($member in $Members) {  
            $out = Get-ADGroup -filter "DistinguishedName -eq '$member'" -properties members  
            $out | Select-Object Name  
            Get-ADNestedGroups -Members $out.Members  
        }  
    }  
      
    $group = "Group Name"  
    $members = (Get-ADGroup -Identity $group -Properties Members).Members  
    Get-ADNestedGroups $members  
    

    Screenshot below is my test in my lab environment, the Organization Management group contain SG1, SG1 contain SG2 and SG3, then I run the above script could view the all group name in the Organization Management group:
    44548-11111.png
    44549-2222.png

    ----------

    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.


1 additional answer

Sort by: Most helpful
  1. Deepak M 1 Reputation point
    2020-12-03T06:18:24.693+00:00

    Hi Austin,

    Please try below script from script gallery.

    https://gallery.technet.microsoft.com/scriptcenter/Get-nested-group-15f725f2

    Regards,
    Deepak


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.