Get the number of users in a particular OU unit and unit.

I am looking for a PowerShell script that will get me:

  • OU's name
  • The number of AD users in each OU .

I found this:

 (Get-ADUser -Filter * -SearchBase "ou=Users,ou=A1,dc=contoso,dc=com").count 

This does exactly what I want, but I would need to enter every OU name. The problem is that we have 100 + OU's . The specific OU that I want to run is contoso.com\cmsg\users in cmsg\users , where 100+ OU's .

+7
powershell active-directory
source share
4 answers

I suggest you come to this from a different angle. Each user of the ad contains information about their parent container. Since you are already returning all users. Let's use this to define counters. The caveat is that if you have a custom OU, they will not appear in the results.

 Get-ADUser -Filter * -Properties CN | Select-Object @{Label='ParentContainer';Expression={$_.Distinguishedname -replace "CN=$($_.cn),"}} | Group-Object -Property ParentContainer | Select-Object Name,Count 

You can, of course, use -SearchBase to narrow the scope of Get-ADUser .

If this is not what you wanted, your next solution will require output from Get-ADOrganizationalUnit .

 $ous = Get-ADOrganizationalUnit -Filter * -SearchBase "ou=Users,ou=CMSG,dc=contoso,dc=com" | Select-Object -ExpandProperty DistinguishedName $ous | ForEach-Object{ [psobject][ordered]@{ OU = $_ Count = (Get-ADUser -Filter * -SearchBase "$_").count } } 
+3
source share

Here is the PowerShell command to get the number of users in a specific organizational unit.

 (Get-ADObject -Filter * -SearchBase "path of your OU").Count 
+1
source share

If the OU is small, an easy way is to select everything in each OU and click on properties. This will result in a total quantity for this unit. You can add each of them after collecting the samples.

0
source share

Running in python - get-aduser will show you all the users

(get-aduser -filter *).count count these users for you. This is all users active or disabled.

-2
source share

All Articles