user770022
user770022

Reputation: 2959

Pull Ad groups and count the users inside each group

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

Answers (1)

Vesper
Vesper

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

Related Questions