title: List Active Directory group hierarchy
body: <p>I'm looking to build a script which would show Active Directory group hierarchy.</p>
  
  <p>Unfortunately simple Get-ADGroupMember $group -Recursive lists only members, not groups.</p>
  
  <p>Example:</p>
  
  <p>Group1 is main group - it has 3 subgroups named GroupA,B,C. So GroupA,B,C is MemberOf Group1.</p>
  
  <p>GroupA has no subgroups</p>
  
  <p>GroupB has 2 subgroups named subGroup1,2</p>
  
  <p>GroupC has 1 subgroup named subGroup3</p>
  
  <p>subGroup1,2,3 has no subgroups</p>
  
  <p>Ideally would be great to have output something like this:</p>
  
  <pre><code>Level1 Level2 Level3   Level4
  Group1 GroupA 
         GroupB subGroup1
                subGroup2
         GroupC subGroup3
  </code></pre>
  
  <p>Of course I have Googled it for, I found two Web-Sites:</p>
  
  <p><a href="http://powershell.com/cs/forums/p/9588/15894.aspx" rel="nofollow noreferrer">http://powershell.com/cs/forums/p/9588/15894.aspx</a></p>
  
  <p><a href="http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27346526.html" rel="nofollow noreferrer">http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27346526.html</a></p>
  
  <p>It's little over my scope to understand them, in first link there is simple script.</p>
  
  <pre><code>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}}
  } 
  </code></pre>
  
  <p>I put <code>$searchGroup = "Administrators"</code> 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.</p>
  
  <p>How to make those scripts to work or make something similar?</p>