Try this:
$startingOU = "OU=EMEA,DC=xxx,DC=zzz"
$hash = [ordered]@{}
$oulist = @()
get-adobject -filter * -property objectcategory -searchbase $startingOU -SearchScope subtree |
ForEach-Object{
if ($_.objectcategory -like "*Organizational-Unit*"){
$oulist += $_.distinguishedName
}
}
ForEach ($ou in $oulist){
$hash['OU'] = $ou
get-adobject -filter * -property objectcategory -SearchBase $ou -SearchScope OneLevel |
ForEach-Object{
if ($_.objectcategory -notlike "*Organizational-Unit*"){
$hash[($_.objectcategory -replace "cn=(.+?),cn=.*$", '$1')]++
}
}
[PSCustomObject]$hash
$hash.Clear()
}