Graph and Group Membership count

I have a bunch of groups whose membership count I need as part of some code I am writing. So far I had a loop like this:

This does the job but is slow. Then I remembered reading somewhere about the $count parameter – in fact, here in the official docs. A screenshot:

And it works for groups too, great. Digging into the groups documentation, I find an example in this page too.

That sounds straight forward then. The Get-MgGroupMember has a -Count parameter but I couldn’t get that working. I tried the following for instance, expecting to have a variable called blahblah with the count… but got nothing!

No worries, lets try Invoke-MgGraphRequest instead.

Nope, nothing! Without ConsistencyLevel: eventual it errors, so pass it in the headers definitely makes a difference. (I noticed this header in the docs above, that’s why I passed it).

Curious about ConsistencyLevel I Googled and came across this interesting blog post. That’s a good read, and has an example of just what I want (the 3rd one below)!

So does that work? Nope!

What the heck! (I tried the beta Url too, not just v1.0 as above)

The same query works fine when using Graph Explorer… so it seems to be an Invoke-MgGraphRequest issue.

Luckily I then stumbled upon this Reddit post where someone asked the same question and got an answer just 12 days ago (good timing for me!). Here’s what they did:

Apparently it’s a bug and the above workaround of saving it to a file instead is the workaround. Nice! I also learnt of [System.IO.Path]::GetTempFileName() in the process… wasn’t aware of that! (So far I’d just create a file with some random numbers in the TEMP location when I wanted a temporary file).