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 http://www.mikepfeiffer.net/2010/01/how-to-install-the-active-directory-module-for-windows-powershell/ 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.

10 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!

  2. May I simply just say what a relief to find someone that actually
    understands what they are discussing over the internet.
    You actually realize how to bring an issue to light and make it important.
    More people need to read this and understand this side of the story.
    I can’t believe you’re not more popular given that you surely possess
    the gift.

    my web page … google

  3. I don’t know whether it’s just me or if perhaps everyone else experiencing
    problems with your blog. It appears like some of the written text on your
    content are running off the screen. Can someone else please comment and
    let me know if this is happening to them too? This could be
    a problem with my web browser because I’ve had this happen before.
    Many thanks

  4. Čeká vás sex zdarma a bez závazků, vyberte
    ѕi z tisíců možných sexy protejšků, které láká láska.
    jste na správné adrese, v naší komunite ѕi jiste vyberete.
    Splnte si predstavy, jaké vás pronásledují ve νašichnejdivocejších snech – erotika na Seznamce
    nezná mezí а hranic.

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>