powershellactive-directory-group

How to use Powershell to find group membership to include members of nested groups and to include the name of the parent group


I am trying to create a CSV of all users in a group, to include members of nested groups, with mapping of which group each member is in.

I found the following powershell command that almost accomplishes this, but I need to also know the name of the nested group for each member of a nested group.

$MembersALL = @()
$MembersRecursiveALL = @()
$Groups = get-adgroup -Filter { Name -like "group name" } | sort Name | Select -expand Name 

Foreach ($Group in $Groups) {

$Members = Get-ADGroupMember $Group | select-object @{Name='Group_Name';Expression={($Group)}}, Name, SamAccountName, objectClass
$MembersRecursive = Get-ADGroupMember $Group -recursive | select @{Name='Group_Name';Expression={($Group)}}, Name, SamAccountName, objectClass
$MembersALL = $MembersALL+$Members
$MembersRecursiveALL = $MembersRecursiveALL+$MembersRecursive


}


$MembersALL | Select * | export-csv '.\My Documents\ADquery\membersall.csv' -notypeinformation -encoding UTF8
$MembersRecursiveALL | Select * | export-csv '.\My Documents\ADquery\membersallrecursive.csv' -notypeinformation -encoding UTF8

Is there a way to modify this to include the the name of the nested group. for each member of a nested group?

Currently this command only includes the original searched group name in the "Group_name" column and not the name of the nested group.


Solution

  • I found a script that does what I was trying to do:

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

    This satisfies my requirement.