arrayspowershellactive-directorywindows-scripting

How to get nested group name from groups in AD using powershell


I have a problem need to example in Active Directory using powershell get all groups that are part of a group and so on recursively.

So if you have group A that has B and B has C Than I need the names of those groups including A.

Also how to fix the following if C also has A I don't want it going in to infinite loop.

Here's my code so far.

$groupname = "a","b","c"

foreach($grouphp in groupname)
{
$groupname += (get-adgroupmember -server $domain -identity $grouphp | where-object {$_.objectclass -like "group"}).name

}

This will add them in to $groupname 1 level also gives me errors if group is not found and if groups found are also groups I'm looking for it will list them multiple times.

Any-help is appreciated.


Solution

  • Please use below script to get users from nested groups :

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