MilesLogan
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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) "
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)
ASKER
Thank you so much for that explanation , you are always very helpful .
ASKER
1: $overall = 0
4: "$($_): total $($current)"
5: $overall += $current
7: "Overall: $($overall)"