Reputation: 2959
I want to pull all AD groups in an OU and then print out each group and the user count thats in that group. The way I currently have it, it just counts how do I get the group name with the count of members?
Import-Module ActiveDirectory
$groups = (Get-ADGroup -Filter {GroupCategory -eq 'security'} -SearchBase 'Path to OU' | select SamAccountName).samaccountname
foreach ($group in $groups){
(Get-ADGroup -Identity $group | select name).count
}
Upvotes: 0
Views: 2717
Reputation: 18747
Use members
attribute, and count that one.
Import-Module ActiveDirectory
$groups = Get-ADGroup -Filter {GroupCategory -eq 'security'} -SearchBase 'Path to OU' -Properties *
foreach ($group in $groups) {$group.members.count}
Or, since you want both,
$groups | select name, {$_.members.count}
Upvotes: 4