A better way to get group count?

I have this code to get the group with the largest count but it takes too long to run and I still don’t get the count(s) enumerated.

$SearchBaseOU = "DC=Company,DC=com"
$Groups = Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' -SearchBase $SearchBaseOU
$results = ForEach($Group in $Groups)
{
    $Members = Get-ADGroupMember -Identity $Group
    $NumberOfMembers = $Members.count
    $props = @{Group=$Group.DistinguishedName
               NumberOfMembers=$NumberOfMembers
               Members=$Members
          }
    New-Object -TypeName psobject -Property $props

}
$results | Sort NumberOfMembers -Descending

Is there a more optimal way to get this info + the count in the results?

Thanks

What do you mean you don’t get the counts enumerated? It seemed to work fine in my tests.

[quote quote=279669]What do you mean you don’t get the counts enumerated? It seemed to work fine in my tests.

[/quote]Well, I get no errors but I want to see the actual numbers. I just see the Groups (and it takes a long time to recurse through so thought I’m not sorting in the right place).

Hmm I do see the numbers (how many members are in each group), with your code. Can you double check the code you’re running? $results.numberofmembers should be a list of numbers.

I’m still waiting for that query to finish running but I think the PIPE is the answer i.e. takes so looooong Plus I get “Get-ADGroupMember : The size limit for this request was exceeded”

Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' | %{ $_.Name;(Get-ADGroupMember $_.Name).Count;"" }

…and this is super fast, however, I want to limit the count to everything over 50 and sort it descending.

Where, after that pipe, would I set that? Something Like:

Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' | %{ $_.Name;(Get-ADGroupMember $_.Name).Count;"" | Where-Object -FilterScript {count -ge "50"}