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.

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. Learning how to make money online from ebooks isn’t a big task.
    ‘ Register at 5 – 10 classified sites and post ads with catchy
    titles. The market for this work from home ideas is huge and constantly
    growing so the potential of making money online now is
    pretty obvious.

  4. This gets the count of a group in one line. My colleague challenged me.

    (Get-ADGroup -Filter {Name -eq “YOURGROUPNAME”} | Get-ADGroupMember | select Name -Unique).count

    If you want to get the unique count of users over several groups with a similar name, for example;

    (Get-ADGroup -Filter {Name -like YOURGROUPNAME*”} | Get-ADGroupMember | select Name -Unique).count

  5. Try this command that lets you export your filtered result into CSV-File to edit in Excel for further anlysis:

    Get-ADGroup -Filter { ObjectClass -like “Group” -and ( SAMAccountName -like “A” -or SAMAccountName -like “B” ) } -SearchBase “OU=MyOU,DC=mydomain,DC=loc” | ForEach-Object { Get-ADGroup -Identity $.SAMAccountName -Properties SAMAccountName,Member,DistinguishedName } | Where { $.Member.Count -ge 5000} | Select-Object SAMAccountName,@{Name=”MemberCount”;Expression={$_.Member.Count.ToString()}},DistinguishedName | Export-Csv -Path C:\Temp\Export001.csv -Encoding “Unicode” -Delimiter “;” -NoTypeInformation

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>