Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MilesLogan

ASKER

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)"
Avatar of oBdA
oBdA

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)"
Thank you so much for that explanation , you are always very helpful .