My company uses Microsoft 365. When I go to admin.microsoft.com, I can see "Teams & groups > Active teams & groups" as navigation item.
Opening one of the groups in the side panel, I can see "Owners" and "Members" as distinct sublists within the "Membership" tab:
I want to retrieve the member lists of all groups, alongside the information whether each member is an owner, using a PowerShell script, from my local PC. Ultimately, I want to export that to CSV, but for now I'm just print-lining to the host. This is my latest attempt:
Connect-MsolService $Groups = Get-MsolGroup -GroupType DistributionList foreach ($Group in $Groups) { $GroupName = $Group.DisplayName $Address = $Group.EmailAddress $GroupId = $Group.ObjectId $Group2 = Get-MsolGroup -ObjectId $GroupId $ManagedBy = $Group2.ManagedBy $GroupMembers = Get-MsolGroupMember -GroupObjectId $GroupId $Fields = $GroupName, $Address, $GroupId, $ManagedBy.EmailAddress + $GroupMembers.EmailAddress $Content = $Fields -join ',' $Content }
The result this gives me is this (notice how the 4th column that should be the $ManagedBy.EmailAddress
is empty):
Foo,[email protected],085d1225-a545-e497-b37b-c6496da4fedd,,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]
There are several things I changed from the original draft before arriving here:
First, I tried to use $GroupMembers.GroupMemberType
to differentiate between "Owner" and "Member", but the possible values of this property are "User, ServicePrincipal, Contact, or Group", so it is User
for members and owners alike, and clearly was not how to do it.
Next, I tried to access $Group.ManagedBy
, hoping to identify at least one of the owners for a start. But the property was empty. Some Microsoft Community Hub post suggested this was because ManagedBy
was only filled when the request was for a single object, hence the $Group2
query and the current form of the script. However, the $ManagedBy
variable is still always empty, for all groups.
So how can I access the information whether a member is an owner, information that the online admin portal obviously has access to?
Additional info:
I also tried to use Get-ADGroup
from the ActiveDirectory
module after enabling RSAT, but this one never asks for or finds a way to connect to the online resource, and the only thing I could find was a supposed Connect-ADServer
cmdlet which isn't recognized as a valid cmdlet on my machine (Get-ADGroup
is, though). Not sure if all that is only meant to be used on the AD server or in the cloud shell.
What further complicates my situation is that according to the Microsoft Learn documentation of MSOnline
it is deprecated, so it recommends "Azure AD Graph" (not sure if that's the same as the ActiveDirectory
module), which according to itself is also already deprecated, hence that is referring to yet another thing, namely Microsoft Graph PowerShell SDK
, which doesn't really look like cmdlets but rather like .NET libraries (but I might be wrong).
It also doesn't help that PowerShell ISE
is also deprecated and everything is a horrible mess in outdated information with deprecation notes linking to also deprecated replacements. I am totally confused and don't even know anymore whether a fixed script using Get-MsolGroup
would work for more than 3 months.
So, I started my quest with "How to get the member role" and have now been down a 10 hour rabbit hole ending up at "What module in which IDE can I even use that isn't deprecated in a few months?"