Count active directory group members

To find information about number of members in Active Directory group, you should use CMDlet:
Get-ADGroupMember

example:

Get-ADGroupMember -Identity "SomeGroup" | Measure-Object | select -ExpandProperty Count

But if you want to get list of all grups in  your Active Directory with information about how many members are in this group, then you should use below example:

Import-Module ActiveDirectory
Get-ADGroup -Filter * |
foreach {
 New-Object -TypeName psobject -Property @{
 GroupName = $_.Name
 MemberCount = Get-ADGroupMember -Identity "$($_.samAccountName)" | Measure-Object | select -ExpandProperty Count
}
} | sort MemberCount

 

As a output you will get list of names of all groups and how many members are in each group.

To save these information to file, change below

 | sort MemberCount

to:

| sort MemberCount  | Export-Csv  -Path d:\scripts\ADGroupsMemberCount.csv  -NoTypeInformation