Skip to content

Instantly share code, notes, and snippets.

@pandieme
Created July 27, 2022 12:53
Show Gist options
  • Save pandieme/b96cd75ae61cfe74b7e037e859317330 to your computer and use it in GitHub Desktop.
Save pandieme/b96cd75ae61cfe74b7e037e859317330 to your computer and use it in GitHub Desktop.
Get the bottom level branches of an Active Directory Organizational Unit
function Get-OrganizationalUnitBranches(
[Microsoft.ActiveDirectory.Management.ADOrganizationalUnit]
$OrganizationalUnit
) {
[array]$Collection = @()
$Children = Get-ADOrganizationalUnit `
-Filter * `
-SearchBase $OrganizationalUnit.DistinguishedName `
-SearchScope OneLevel
if (!$Children) {
return $OrganizationalUnit
}
foreach ($Child in $Children) {
$Collection += Get-OrganizationalUnitBranches -OrganizationalUnit $Child
}
return $Collection
}
Get-OrganizationalUnitBranches -OrganizationalUnit (Get-ADOrganizationalUnit -Identity $RootOuDistinguishedName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment