Get The Count of the Number of Users in an AD Group

I was challenged at work today to determine  the number of users in an Active Directory group. I figured the best way was to break out PowerShell and see what I could find (I’m sorry but I’m learning PowerShell so things are going to be very PowerShell centered for a while :-) ). I found that in the ActiveDirectory PowerShell module (see for instructions on how to install this) the Get-ADUser cmdlet works really well for running basic searches in AD.

The first step is to import the module:

Import-Module ActiveDirectory

Then you can run a search on the user information you want for example this returns the count of all users in a group:

(get-aduser -filter {memberof -recursivematch "CN=Group,OU=Users,DC=contoso,DC=local"}).count

This returns the count of all group members who have an expiration date:

(get-aduser -properties AccountExpirationDate -filter {memberof -recursivematch "CN=Group,OU=Users,DC=contoso,DC=local"} | where {$_.AccountExpirationDate -ne $null}).count

The cool things about the get-aduser cmdlet is that it automatically pulls common fields (username, surname, etc.) but by adding items to the -property field it will return additional items. If there are other searches you find useful please add them to the comments below.

4 thoughts on “Get The Count of the Number of Users in an AD Group

  1. I am using Get-ADUser a bit differently so thought of sharing the same

    Get-ADUser -filter {Name -like “*”} -SearchBase “OU=Peons,DC=india,DC=ca,DC=com”

    Apart from
    -filter {memberof -recursivematch “CN=Group,OU=Users,DC=contoso,DC=local”}

    you can also use
    -filter {Name -like “*”} -SearchBase “OU=Peons,DC=india,DC=ca,DC=com”

    • That will only return the members of the group – so if you have any sub-groups, it won’t enumerate those.

      I’m wondering if the original filter method suggested by the author would include duplicates across that group…

      Just tested it – apparently it does account for duplicates across sub-groups too!

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>