Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 845
narue1992 Avatar asked Nov 27 '22 07:11

narue1992


2 Answers

The Get-ADGroupMember cmdlet would solve this in a much more efficient way than you're tying.

As an example:

$users = @(Get-ADGroupMember -Identity 'Group Name')
$users.count
132

EDIT:

In order to clarify things, and to make your script simpler. Here's a generic script that will work for your environment that outputs the user count for each group matching your filters.

$groups = Get-ADGroup -filter {(name -like "WA*") -or (name -like "workstation*")}
foreach($group in $groups){
  $countUser = @(Get-ADGroupMember $group.DistinguishedName).count
  Write-Host "The group $($group.Name) has $countUser user(s)."
}
like image 70
Alex McKenzie Avatar answered Dec 06 '22 10:12

Alex McKenzie


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

like image 28
Zsolt Varga Avatar answered Dec 06 '22 08:12

Zsolt Varga