Count User objects for multiple OUs

Can someone help me with a script that will count the user objects in multiple OUs ?

I can get the total of one OU at a time but not multiple OUs at a time .
I would like the output to be something like .

OU1 total 123
OU2 total 456
OU 3 total 789
LVL 2
MilesLoganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try it like this:
$overall = 0
"OU=OU1,DC=contoso,DC=com", "OU=OU2,DC=contoso,DC=com" | ForEach-Object {
	$current = (Get-ADUser -Filter * -SearchBase $_ | Measure-Object).Count
	"$($_): total $($current)"
	$overall += $current
}
"Overall: $($overall)"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
that worked just as I needed .. can you take a minute and explain what these do ?

1:    $overall = 0
4:   "$($_): total $($current)"
5:    $overall += $current
7:    "Overall: $($overall)"
0
oBdACommented:
1: Pretty obvious, isn't it? Just a variable assignment. This will hold the user count over all OUs processed.
4: Outputs a string containing the loop variable of "ForEach-Object" ($_) and the user count for the current OU.
5: Adds the current user count to the overall user count.
7: Same as 4.
In 4 and 7, the variables are wrapped in subexpressions $() - though this is not required for $current ("$($_): total $current" would work as well), since it is a simple variable, I prefer to consistently use subexpressions when accessing variables inside a string. They're required when accessing properties of an object, though ("SomeProperty: $(SomeObject.SomeProperty)"), or even complete expressions: "Windows folder: $((Get-Item C:\Windows).LastWriteTime)"
0
MilesLoganAuthor Commented:
Thank you so much for that explanation , you are always very helpful .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.