How to Get Full LDAP Path of Active Directory Groups Using PowerShell


8 views

When working with Active Directory in large enterprise environments, locating the exact organizational unit (OU) path of a specific group can be challenging, especially in complex directory structures with nested OUs. The DistinguishedName attribute actually contains this path information, but it's not immediately obvious how to extract and format it properly.

The Active Directory module for PowerShell provides the perfect tool for this task. Your example already shows the basic command:

Get-ADGroup "AXX G Doc Users"

This returns all properties of the group, including the crucial DistinguishedName:

DistinguishedName : CN=AXX G Doc Users,OU=Groups,OU=AXX,OU=AT,OU=Europe,OU=COMP Group,DC=comp,DC=com

To make the path more human-readable, you can parse the DistinguishedName:

$group = Get-ADGroup "AXX G Doc Users"
$pathParts = $group.DistinguishedName -split ','
$ouPath = ($pathParts | Where-Object { $_ -like 'OU=*' } | ForEach-Object { 
    $_.Substring(3) 
}) -join '\'

Write-Output "Group Path: $ouPath"

This would output something like:

Group Path: COMP Group\Europe\AT\AXX\Groups

For more complex scenarios, you might need to search across multiple domains or include additional properties:

Get-ADGroup -Filter { Name -like "*Doc Users*" } -Properties CanonicalName | 
Select-Object Name, @{Name="Path";Expression={$_.CanonicalName -replace "/$($_.Name)$"}}

An alternative approach is to use the CanonicalName attribute which already represents the path in forward-slash format:

(Get-ADGroup "AXX G Doc Users" -Properties CanonicalName).CanonicalName

This returns the path as:

comp.com/COMP Group/Europe/AT/AXX/Groups

Here's a comparison of different techniques:

$group = Get-ADGroup "AXX G Doc Users" -Properties *

[PSCustomObject]@{
    DistinguishedName = $group.DistinguishedName
    CanonicalPath = $group.CanonicalName
    ParsedOUs = ($group.DistinguishedName -split ',' | 
                Where-Object { $_ -like 'OU=*' } | 
                ForEach-Object { $_.Substring(3) }) -join '\'
}

This information becomes particularly useful when:

  • Documenting AD structures
  • Troubleshooting group policy inheritance
  • Migrating groups between OUs
  • Auditing security permissions

For groups with special characters in their names, use proper escaping:

Get-ADGroup -Identity "AXX/G Doc Users" -Properties CanonicalName

When working with extensive Active Directory structures, locating the complete hierarchical path of a specific security group can be crucial for administration tasks. The DistinguishedName property shown in PowerShell output actually contains the full path information, but it's encoded in LDAP format.

The DistinguishedName follows this pattern:

CN=GroupName,OU=OrganizationalUnit,OU=ParentOU,...,DC=domain,DC=com

Here's a PowerShell function to convert the DN to a readable path:

function Get-ADGroupPath {
    param(
        [Parameter(Mandatory=$true)]
        [string]$GroupName
    )
    
    $group = Get-ADGroup -Identity $GroupName -Properties DistinguishedName
    $dnParts = $group.DistinguishedName -split '(?

Another method is to use the CanonicalName property which shows the path in forward-slash format:

Get-ADGroup "AXX G Doc Users" -Properties CanonicalName | 
Select-Object -ExpandProperty CanonicalName

Applying this to our example group "AXX G Doc Users":

PS> Get-ADGroupPath -GroupName "AXX G Doc Users"
COMP Group\\Europe\\AT\\AXX\\Groups

Knowing the exact group path is essential when:

  • Creating new groups in specific locations
  • Generating reports of group structures
  • Troubleshooting permission issues
  • Migrating groups between domains

For complex scenarios where you need to handle escaped commas in OU names:

$dn = "CN=AXX G Doc Users,OU=Groups,OU=AXX,OU=AT,OU=Europe,OU=COMP Group,DC=comp,DC=com"
[adsi]"LDAP://$dn" | Select-Object -ExpandProperty canonicalName