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

**URL:** <https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729>\
**Category:** PowerShell Help\
**Created:** [April 22, 2025, 7:32pm UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729 "2025-04-22T19:32:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbrain1978](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@tbrain1978](https://forums.powershell.org/u/tbrain1978)\
**Post date:** [April 22, 2025, 7:32pm UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729/1 "2025-04-22T19:32:28Z")

</div>

Hi 🙂

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?

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [April 22, 2025, 8:49pm UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729/2 "2025-04-22T20:49:56Z")

</div>

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](https://forums.powershell.org/t/guide-to-posting-code/14928)

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:

```Powershell
$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:

```Powershell
[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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 22, 2025, 9:15pm UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729/3 "2025-04-22T21:15:32Z")

</div>

Tim,  
Welcome to the forum. 👋🏼

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

```auto
$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
        }
    }

```

---

<div class="post-metadata">

**Author:** ![tbrain1978](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@tbrain1978](https://forums.powershell.org/u/tbrain1978)\
**Post date:** [April 23, 2025, 6:19am UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729/4 "2025-04-23T06:19:38Z")

</div>

> [@tbrain1978](#):
>
> ```auto
> $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
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![tbrain1978](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@tbrain1978](https://forums.powershell.org/u/tbrain1978)\
**Post date:** [April 23, 2025, 6:21am UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729/5 "2025-04-23T06:21:33Z")

</div>

HI Olaf 😀 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 🤞

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 23, 2025, 6:22am UTC](https://forums.powershell.org/t/pulling-back-a-count-of-direct-group-members-where-objectclass-is-group/25729/6 "2025-05-23T06:22:25Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
