Get-sqmADGroupMembersRecursive Configuration

Enhanced version of Get-sqmADGroupMembers with configurable nesting depth. Resolves nested groups up to -Depth levels. Resolves real AD displayName for user accounts. Cycle-detection via visited-hashtable. Falls back to LDAP when the ActiveDirectory module is unavailable.

Execution Flow

START ADSI RootDSE check + init $processedGroups hashtable ADSI OK? throw ERROR NO foreach $group in $GroupName [pipeline] Resolve domain, clean group name Expand-GroupMembers (recursive helper, Depth-limited, cycle-safe) Check $Visited → skip if already processed Try: AD module → Get-ADGroupMember -Recursive (depth 0) or -Direct (depth >0) → Get-ADUser for real DisplayName Catch: LDAP fallback → DirectorySearcher + recurse nested groups DisplayName fallback chain: displayName → cn → sAMAccountName Return members[] with Depth property Sort-Object -Unique by SamAccountName ShouldProcess → write TXT + CSV with Depth column Build result: GroupName/Depth/MemberCount/Members[] Return $allResults[] DONE

Synopsis

Enhanced recursive member resolution with a configurable -Depth limit (0–10). At depth 0, Get-ADGroupMember -Recursive is used directly (full recursion via AD module). At deeper levels, direct members are fetched and nested groups are expanded manually. For user accounts, Get-ADUser -Properties DisplayName is called to show the person's real name. A visited-hashtable prevents infinite loops in circular group memberships.

See also: Get-sqmADGroupMembers (simple, no depth control) and Get-sqmADMemberGroups (inverse: which groups contain a user).

Syntax

Get-sqmADGroupMembersRecursive
    -GroupName <String[]>    # required, pipeline
    [-Domain <String>]
    [-Depth <Int>]           # 0-10, default 2
    [-OutputPath <String>]

Parameters

ParameterTypeDefaultDescription
-GroupNameString[]RequiredAD group name(s). Accepts pipeline input.
-DomainStringauto-detectAD domain. Auto-detected from current domain if omitted.
-DepthInt2Maximum nesting depth for group expansion (0–10). 0 = flat list only.
-OutputPathStringC:\System\WinSrvLog\MSSQLDirectory for TXT and CSV report files.

Return Value

PropertyDescription
GroupNameResolved group name.
DepthMaximum depth used for expansion.
MemberCountUnique member count after deduplication.
MembersList with SamAccountName, DisplayName, ObjectClass, Depth.
TxtFile / CsvFileWritten report file paths.
StatusOK / Warning / Error.

Examples

# Resolve up to 3 nesting levels
Get-sqmADGroupMembersRecursive -GroupName "DL_SQL_Admins" -Depth 3

# Flat list only (no nested group expansion)
Get-sqmADGroupMembersRecursive -GroupName "DL_SQL_Admins" -Depth 0

# Multiple groups via pipeline
"DL_SQL_Admins","DL_SQL_Readers" | Get-sqmADGroupMembersRecursive -Depth 2