Count active directory group members

This post is also available in: Polish

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

 

Print Friendly
Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>