Get AD objects number from OUs sorting by object category

Sylvain MALAGRE 21 Reputation points
2023-04-18T12:19:49.8433333+00:00

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

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,999 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,167 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 45,111 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()
        }
    
    0 comments No comments

  2. 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
    

  3. Sylvain MALAGRE 21 Reputation points
    2023-04-20T12:57:49.8133333+00:00

    With the file it is better Capture

    0 comments No comments