Pulling back a count of direct group members where objectClass is group

Hi :slight_smile:

I’m new to Powershell, and am building a script to pull various information related to group membership.

Everything works as expected, except for when I am trying to pull a count of domain groups that are direct members of a domain group. It works fine when I have more than 1 domain group that is a direct member i.e. returns 2, 3, 4 etc., but if there is only 1 domain group that is a direct member then $DirectGroupsCount is always blank. $groups is pulling back the domain group that is a direct member and displaying in the console. Any advice?

$Groups = @()
$Groups +=Get-ADGroupMember "<domain group>" |
Where-Object {$_.objectClass -eq "group"}
Write-Host $Groups -ForegroundColor Magenta
$DirectGroupsCount = ($Groups | Where-Object { $_.objectClass -eq 'group' }).Count
Write-Host "Direct Groups Count: $DirectGroupsCount" -ForegroundColor Green

hi @tbrain1978 and welcome to the forum.
First things first if you wouldn’t mind editing your post and formatting your code as “Preformatted text” that would make it a lot easier for people to help.
Guide to Posting Code

This isn’t an answer to your question but a bit of constructive criticism. You’re declaring an empty array and then using the += operation to add objects to that array. It would be less work, and faster, to capture the output of the operation with your array definition like this:

$Groups = Get-AdGroupMember <something>

Get-AdGroupMember specifically returns the members of a given group, then it looks like we’re piping that to Where-Object to filter the output to only include groups that are a member of that provided group. Those go in to our $Groups variable.

Later when you’re defining $DirectGroupsCount there shouldn’t be any need to pipe it to Where-Object again since $Groups should only contain group objectclass objects anyway.
Unfortunately, if there’s only one group object as a member of a group your $Groups variable wouldn’t be an array with a ‘count’ property unless you did it like you have in your code. How I would handle this is this way:

[Array]$Groups = Get-ADGroupMember -Identity $GroupName | Where-Object {$_.Objectclass -eq "Group"}
Write-Host $Groups -ForegroundColor Magenta
Write-Host "Direct Groups Count: $($Groups.count)" -ForegroundColor Green

the [Array] syntax is strongly typing the variable $Groups to make sure it’s always an array, even if it only contains one thing. That way the count property will exist.

Tim,
Welcome to the forum. :wave:t3:

I don’t know how many groups you want to query this way but I’d query a bunch of them at once.

$SearchBase = 'OU=Europe,DC=Contoso,DC=de'
Get-ADGroup -Filter * -SearchBase $SearchBase |
    ForEach-Object {
        [PSCustomObject]@{
            Name             = $_.SamAccountName
            MemberGroupCount = @(Get-ADGroupMember -Identity $_.SamAccountName | Where-Object -Property 'ObjectClass' -EQ -Value 'group').Count
        }
    }
1 Like

Thanks so much for this. Makes sense now having being going around in circles for hours :grinning_face:

HI Olaf :grinning_face: Thanks for your help with this, and yes, ultimately I’ll need to be querying all of the groups on the domain. I’m going to try and feed this into my script and run a few tests to sanity check that the output is as expected. Thanks so much for your help with this :crossed_fingers: