Created
July 27, 2022 12:53
-
-
Save pandieme/b96cd75ae61cfe74b7e037e859317330 to your computer and use it in GitHub Desktop.
Get the bottom level branches of an Active Directory Organizational Unit
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-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