Question:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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 } |
Answer:
The Get-ADGroupMember
cmdlet would solve this in a much more efficient way than you’re tying.
As an example:
1 2 3 4 |
$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.
1 2 3 4 5 6 |
$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)." } |