Home Β» Office 365 Β» Get-MsolGroupMember – Get All Group Members in Office 365

Get-MsolGroupMember – Get All Group Members in Office 365

The Get-MsolGroupMember cmdlet in PowerShell gets the group members of the specified group in Microsoft Office 365. The members can be either users or groups.

The syntax to retrieve group members of the group is given below.

Get-MsolGroupMember
   [-GroupObjectId <Guid>]
   [-All]
   [-MemberObjectTypes <String[]>]
   [-SearchString <String>]
   [-MaxResults <Int32>]
   [-TenantId <Guid>]
   [<CommonParameters>]

In this article, we will discuss how to use the Get-MsolGroupMember to get group members of the specified group and list Office 365 groups of which the user is a member with PowerShell.

Let’s understand the Get-MsolGroupMember cmdlet with examples.

How to Get All Members of a Group in Office 365

To get all members of a group in Office 365, use the Get-MsolGroupMember cmdlet with -GroupObjectId parameter. The -GroupObjectId parameter specifies the unique ID of the group from which we want to get members.

Get-MsolGroupMember -GroupObjectId f876f049-21b1-450d-8961-7e63f49f232e

This command retrieves all members of the specified group β€œf876f049-21b1-450d-8961-7e63f49f232e”.

Get Members of Office 365 Groups
Get Members of the Office 365 Group

To export all members of the group to the CSV file, use the Export-CSV file.

PS C:\> Get-MsolGroupMember -GroupObjectId f876f049-21b1-450d-8961-7e63f49f232e | Export-Csv -Path D:\PS_delete\group_members.csv -NoTypeInformation 

How to Get All Members of Office 365 Group with Where-Object

To get all members of Office 365 MsolGroups, use the Get-MsolGroup cmdlet in PowerShell to retrieve the specific group object. The Get-MsolGroupMember command uses the group object to check the group object ID to retrieve all members of a group.

# Get a MsolGroup object
$Group = Get-Msolgroup -All | Where-Object {$_.DisplayName -eq "Finance"} 

# Retrieve members of Msol group
Get-MsolGroupMember -GroupObjectId $Group.ObjectId

In the above PowerShell script, the Get-MsolGroup uses the -All parameter to retrieve all groups in Office 365 and pipes them to Where-Object cmdlet to filter the group where the display name is equal to β€œFinance” and returns the group object. This command stores the β€œFinance” group in the $Group variable.

The Get-MsolGroupMember cmdlet in PowerShell uses the -GroupObjectId cmdlet to specify the group objectId and retrieve all members of a β€œFinance” group.

The output of the above command is given below.

PS C:\> $Group = Get-Msolgroup -All | Where-Object {$_.DisplayName -eq "Finance"}                                            

PS C:\> $Group                                                                                                               
ObjectId                               DisplayName                  GroupType                    Description
--------                               -----------                  ---------                    -----------
008f363f-5b3a-4899-8004-a2d140426802   Finance                      DistributionList             This is the Finance Group


PS C:\> Get-MsolGroupMember -GroupObjectId $Group.ObjectId                                                                   
GroupMemberType EmailAddress                       DisplayName
--------------- ------------                       -----------
User            [email protected] Samer Amberson

How to List Office 365 Groups Which User is Member of with PowerShell

To list Office 365 groups to which a user belongs using PowerShell, use the foreach loop to iterate the groups in Office 365 and check if a user is a member of the group with the Get-MsolGroupMember command.

foreach ($Group in (Get-MsolGroup -all)) 
 {
   if (Get-MsolGroupMember -all -GroupObjectId $Group.ObjectId | where {$_.Emailaddress - 
   eq "[email protected]"}) {$Group.Displayname}
}    

The foreach loop iterates over all Office 365 groups retrieved using the Get-MsolGroup cmdlet. The Get-MsolGroupMember command gets all the members of the specific group and pipes them to the Where-Object cmdlet to check if the user email address is equal to β€œ[email protected]β€œ. If the user belongs to the group, it prints the group name.

The output of the above command prints the Office 365 groups the user belongs to.

PS C:\> foreach ($Group in (Get-MsolGroup -all)) {if (Get-MsolGroupMember -all -GroupObjectId $Group.ObjectId | where {$_.Emailaddress -eq "[email protected]"}) {$Group.Displayname}}   

Security
Finance

What is the Output of the Get-MsolGroupMember cmdlet?

The Get-MsolGroupMember cmdlet in PowerShell returns the objects that contain the following information:

  • GroupMemberType: The group member type (User, Group, Contact or ServicePrincipal)
  • EmailAddress: The primary email address of the group member.
  • DisplayName: The display name of the group.
  • ObjectId: The unique ID of the group.

Conclusion

I hope the above article on how to use the Get-MsolGroupMember cmdlet in PowerShell to get all members of the group is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Related Links

Add-MsolGroupMember

Remove-MsolGroupMember