I'm looking to build a script which would show Active Directory group hierarchy.
Unfortunately simple Get-ADGroupMember $group -Recursive lists only members, not groups.
Example:
Group1 is main group - it has 3 subgroups named GroupA,B,C. So GroupA,B,C is MemberOf Group1.
GroupA has no subgroups
GroupB has 2 subgroups named subGroup1,2
GroupC has 1 subgroup named subGroup3
subGroup1,2,3 has no subgroups
Ideally would be great to have output something like this:
Level1 Level2 Level3 Level4
Group1 GroupA
GroupB subGroup1
subGroup2
GroupC subGroup3
Of course I have Googled it for, I found two Web-Sites:
http://powershell.com/cs/forums/p/9588/15894.aspx
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27346526.html
It's little over my scope to understand them, in first link there is simple script.
function Get-GroupHierarchy ($searchGroup)
{
import-module activedirectory
$groupMember = get-adgroupmember $searchGroup | sort-object objectClass -descending
foreach ($member in $groupMember)
{Write-Host $member.objectclass,":", $member.name;
if ($member.ObjectClass -eq "group")
{Get-GroupHierarchy $member.name}}
}
I put $searchGroup = "Administrators"
before the script, but script doesn't show any results. It has 3 sub-groups if I do Get-ADGroupMember. Probably I don't know how to work with functions.
How to make those scripts to work or make something similar?