title: Powershell: count members of a AD group
body: <p><strong>RECIEVED AN EXAMPLE I COULD USE AND CORRECTED CODE</strong></p>
  
  <p>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.</p>
  
  <p>I've tried $members.count, $member.count and $string.count in my foreach loop but nothing prints out. Please help</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>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: <strong>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.</strong></p>
  
  <pre><code>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
      }
    }
  </code></pre>
  
  <p><strong>Below is my finished code. I just made separate .txt files for each things I needed and will just separately import them into excel.</strong> <strong>QUESTION FIXED</strong></p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>I add a counter in original script if I want it to get count as well</p>
  
  <pre><code>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
  }
  </code></pre>