[powershell] Powershell: count members of a AD group

RECIEVED AN EXAMPLE I COULD USE AND CORRECTED CODE

My current question is how to count amount of members in a group versus printing out all members of a group (which includes their ID name or PC name). The commented out code prints each member. I just want to count them.

I've tried $members.count, $member.count and $string.count in my foreach loop but nothing prints out. Please help

Import-Module ActiveDirectory

$pathEmpty = "C:\Temp\groupsEmpty.txt"

Clear-Content $pathEmpty

$Header = `
"Group ID Name" + "|" + `
"Display Name" + "|" + `
"Description" + "|" + `
"Members"


#Write out the header
$Header | Out-File $pathEmpty -Append


$emptys = get-adgroup -properties name, displayname, description, members -filter  name -like "WA*" -or name -like "workstation*"} `
 | Select name, displayname, description, members

foreach ($empty in $emptys)
{
#clears previous 
$members = ""
foreach ($member in $empty.members)
  {
    $string = $member.substring(3,$member.indexof(",")-3)
    #$members = $members + ":" + $string
    $string.count 
  }
$listing =`
$empty.Name + "|" + `
$empty.DisplayName + "|" + `
$empty.Description + "|" + `
$members

$listing | Out-File $pathEmpty -Append
}

I made an edit based on Alex McKenzie's comment. The initial example you gave me made issues. I tried adding some edits to it but I get a error: Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

foreach ($empty in $emptys)
{
#clears previous 
$members = ""
foreach ($member in $empty.members)
  {
    #$string = $member.substring(3,$member.indexof(",")-3)
    #$members = $members + ":" + $string

    if($member -eq $null){
        $users = 0
        }
    else{
        $users = (Get-ADGroupMember -Identity $($_.DistinguishedName)).Count
        #$users.count
    }
  }

Below is my finished code. I just made separate .txt files for each things I needed and will just separately import them into excel. QUESTION FIXED

Import-Module ActiveDirectory

##########################################################################
#This Section involves filtering WA groups and retriving their information
#In the info section (aka Notes section) is very important to look
#over if groups require approvals or not

##########################################################################

$pathAll = "C:\Temp\groupsEALL.txt"

Clear-Content $pathALL

$Header = `
"Group ID Name" + "|" + `
"Description" + "|" + `
"Notes Field" + " |" + "Members"


#Write out the header
$Header | Out-File $pathALL -Append

 $emptys = get-adgroup -properties name, description, members, info -filter {name -like "WA*" -or name -like "workstation*"} `
 | Select name, description, members, info

foreach ($empty in $emptys)
{

#clears previous 
$members = ""
foreach ($member in $empty.members)
  {
    #$users = (Get-ADGroupMember -Identity $member.DistinguishedName).Count
    $string = $member.substring(3,$member.indexof(",")-3)
    $members = $members + ":" + $string
   }

$listing =`
$empty.Name + "|" + `
$empty.Description + "|" + `
$empty.Info + "|" + `
$members

$listing | Out-File $pathALL -Append
}

################################################################
#This next section will determine the member count in the groups

################################################################

$pathCount = "C:\Temp\Groupcount.txt"
Clear-Content $pathcount

$groups = Get-ADGroup -filter {(name -like "WA*") -or (name -like "workstation*")}
foreach($group in $groups){
 #initializing
 $countUser = ""
   $countUser = ((get-Adgroup $group -properties members).members).count


"The group $($group.Name) has $countUser user(s)." | Out-File $pathCount -Append
}

I add a counter in original script if I want it to get count as well

foreach ($empty in $emptys)
{

#clears previous 
$members = ""
#initialize member counting
$count = 0
foreach ($member in $empty.members)
  {
    #counts how many members
    $count += 1
    $countSum = "The group $($group.Name) has $count user(s)."
    $string = $member.substring(3,$member.indexof(",")-3)
    $members = $members + ":" + $string
   }
    $listing =`
$empty.Name + "|" + `
$empty.Description + "|" + `
#$empty.Info + "|" + `
$empty.info + "|" + `
$members + "|" + $countSum

$listing | Out-File $pathALL -Append
}

This question is related to powershell count

The answer is


In Powershell, you'll need to import the active directory module, then use the get-adgroupmember, and then measure-object. For example, to get the number of users belonging to the group "domain users", do the following:

Import-Module activedirecotry
Get-ADGroupMember "domain users" | Measure-Object

When entering the group name after "Get-ADGroupMember", if the name is a single string with no spaces, then no quotes are necessary. If the group name has spaces in it, use the quotes around it.

The output will look something like:

Count    : 12345
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Note - importing the active directory module may be redundant if you're already using PowerShell for other AD admin tasks.


easy way to do it: To get the actual user count:

$ADInfo = Get-ADGroup -Identity '<groupname>' -Properties Members
$AdInfo.Members.Count

and you get the count easily, it is pretty fast as well for 20k+ users too


$users = Get-ADGroupMember -Identity 'Client Services' -Recursive ; $users.count

The above one-liner gives a clean count of recursive members found. Simple, easy, and one line.


Try:

$group = Get-ADGroup -Identity your-group-name -Properties *

$group.members | count

This worked for me for a group with over 17000 members.


Something I'd like to share..

$adinfo.members actually give twice the number of actual members. $adinfo.member (without the "s") returns the correct amount. Even when dumping $adinfo.members & $adinfo.member to screen outputs the lower amount of members.

No idea how to explain this!


Every response has missed one detail. What if the group only has 1 user.

$count = @(get-adgroupmember $group).count

Put the command in the @() wrapper so that the result is an array no matter what, so even if it is 1 item, you get a count.


If you cannot utilize the ActiveDirectory Module or the Get-ADGroupMember cmdlet, you can do it with the LDAP "in chain"-matching rule:

$GroupDN = "CN=MyGroup,OU=Groups,DC=mydomain,DC=tld"
$LDAPFilter = "(&(objectClass=user)(objectCategory=Person)(memberOf:1.2.840.113556.1.4.1941:=$GroupDN))"

# Ideally using an instance of adsisearcher here:
Get-ADObject -LDAPFilter $LDAPFilter

See MSDN for additional LDAP matching rules implemented in Active Directory


How about this?

Get-ADGroupMember 'Group name' | measure-object | select count