With the file it is better
Get AD objects number from OUs sorting by object category
Sylvain MALAGRE
21
Reputation points
Hi, I need tour help please I am a neeby to PowerShell. I ha e this structure : toto.com
EMEA (First OU) France (subOU of EMEA) Spain (subOU of EMEA)
I would like to have in a pane for each subOU the number of Users, Computers, Groups ... sorting by object category via PowerShell Can you help me to do it please ? Thanks
Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
3 answers
Sort by: Most helpful
-
Sylvain MALAGRE 21 Reputation points
2023-04-20T12:57:49.8133333+00:00 -
Sylvain MALAGRE 21 Reputation points
2023-04-20T12:57:13.88+00:00 Hi, Thank you but it does not work. Here is a script that works but the problem is that it displays in the pane all the objects number per category for all the subOU. I just want the number for the 2nd level subOU like this :
Toto.com France DJ Servers Groups Users NC PA ... I just want the number for the DJ, NC, PA ... subOU only -
Rich Matheisen 48,116 Reputation points
2023-04-18T19:28:36.9933333+00:00 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() }