Home » PowerShell » Get-AdGroupMember Count for Users and Groups

Get-AdGroupMember Count for Users and Groups

To get the adgroupmember count for users and groups members of adgroup, use the Get-AdGroupMember cmdlet with the Measure-Object command to get adgroupmember count.

The Get-AdGroupMember cmdlet in PowerShell gets members of an active directory group. The Measure-Object cmdlet performs the calculation like count and average on the property value of objects.

In this article, we will discuss how to get adgroupmember count for users and groups in the active directory.

Get-AdGroupMember Count for AdGroup

To get the adgroup member count for the adgroup that contains users and groups in it, use the Get-AdGroupMember cmdlet with the Measure-Object cmdlet.

(Get-ADGroupMember -Identity "SALESLEADER") |Measure-Object | Select Count

In the above PowerShell script, the Get-AdGroupMember uses the Identity parameter to specify the adgroup name “SALESLEADER” and pipes the adobjects to the Measure-Object cmdlet to get adgroup member count.

The output of the above script to get the adgroup members count in the group is:

Get-AdGroupMember Count
Get-AdGroupMember Count

Get AdGroupMember Count for User in ADGroup

To get the adgroupmember user count in the adgroup, use the Get-AdGroupMember cmdlet and use the Where condition to filter for ObjectClass is equal to user.

(Get-ADGroupMember "SALESLEADER" | where {$_.objectclass-eq "user"}) | Measure-Object | Select Count

In the above PowerShell script, the Get-AdGroupMember gets all the members from the adgroup “SALESLEADER” and pipes them to the Where condition.

Where condition filters result to get users only from the adgroup and use the Measure-Object cmdlet to get adgroupmember aduser count.

The output of the above script to count user in adgroup using Get-AdGroupMember is:

PS C:\> (Get-ADGroupMember "SALESLEADER" | where {$_.objectclass-eq "user"}) | Measure-Object | Select Count

Count
-----
    3


PS C:\>

Cool Tip: How to get adgroupmember name and email address in PowerShell!

Get-AdGroupMember Count for Group in PowerShell

To get adgroupmember count for groups in the specified active directory group, use the Get-AdGroupMember cmdlet with Measure-Object to get the count for the group in an ad group.

(Get-ADGroupMember "SALESLEADER" | where {$_.objectclass-eq "group"}) | Measure-Object | Select Count

In the above PowerShell script, the Get-AdGroupMember gets all the members from the adgroup “SALESLEADER” and uses the where condition to filter the members for groups only.

Using the Measure-Object cmdlet, it performs the calculation for properties on the value of adgroup object and returns the adgroup member count.

The output of the above PowerShell script to find adgroup member count is:

PS C:\> (Get-ADGroupMember "SALESLEADER" | where {$_.objectclass-eq "group"}) | Measure-Object | Select Count

Count
-----
    1


PS C:\>

Cool Tip: How to get ad group members from multiple groups in PowerShell!

Conclusion

I hope the above article on how to get adgroupmember count for users and groups in PowerShell is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.