Created
June 14, 2023 08:56
-
-
Save junecastillote/d0e27573dbbb87e863cca4488062bdb8 to your computer and use it in GitHub Desktop.
Find Active Directory Nested Group Members
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-AdGroupNestedMember { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[Microsoft.ActiveDirectory.Management.ADGroup] | |
$Identity, | |
[Parameter()] | |
[switch] | |
$ShowTop | |
) | |
try { | |
$group = Get-ADGroup -Identity $Identity -ErrorAction Stop | |
if ($ShowTop) { | |
$group | |
} | |
$members = Get-ADGroupMember $group -ErrorAction Stop | |
# Loop through the members | |
foreach ($member in $members) { | |
# Check if the member is a group | |
if ($member.objectClass -eq "group") { | |
# Recursively call the function for nested groups | |
Get-AdGroupNestedMember -Identity $member.Name -ShowTop | |
} | |
else { | |
# Output | |
$member | |
} | |
} | |
} | |
catch { | |
$_.Exception.Message | Out-Default | |
return $null | |
} | |
} | |
Function Get-AdUserNestedMemberOf { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[Microsoft.ActiveDirectory.Management.ADUser] | |
$Identity | |
) | |
try { | |
$adUser = Get-ADUser -Identity $Identity -ErrorAction Stop | |
Get-ADGroup -LDAPFilter "(member:1.2.840.113556.1.4.1941:=$($adUser.DistinguishedName))" -ErrorAction Stop | |
} | |
catch { | |
$_.Exception.Message | Out-Default | |
return $null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment