Retrieve Microsoft 365 Group Members Using Graph PowerShell

This guide demonstrates how to use Microsoft Graph PowerShell to retrieve members of Microsoft 365 groups. Learn to list group members, filter by criteria, and export membership details with practical examples.

You can use the Microsoft Graph PowerShell SDK to fetch and display Microsoft 365 group members. Here's a Graph PowerShell script that prompts you for the group's ID, retrieves the members of that group, and display their basic information on the console.


Prerequisites

  • You need to install the Microsoft Graph PowerShell SDK. Install-Module Microsoft.Graph -Scope CurrentUser is the command.
  • You need to connect to the Microsoft Graph PowerShell Module with the necessary permissions. Connect-MgGraph -Scopes "Group.Read.All", "User.Read.All" is the command.

Get Microsoft 365 Members Graph PowerShell Script

The script prompts you to enter the Group ID. Then it uses the Get-MgGroupMember cmdlet to fetch all the Microsoft 365 members of the group, loops through them and displays their DisplayName, Mail and JobTitle on the PowerShell console.

Graph PowerShell Script for listing Microsoft 365 Group M.

When you execute the script, you should get the following response:

Graph PowerShell Script for listing Microsoft 365 Group Owners Output.

How the Script Works?

The Get Microsoft 365 Group Members Graph PowerShell script does the following:

  • Module Installation and Import: The script begins by checking if the Microsoft Graph module is installed. If not, it installs and then imports it.
  • Connection: Connects to Microsoft Graph using appropriate permissions. It requires the Group.Read.All permission to read group memberships and User.Read.All to access user details.
  • Group ID Input: It prompts you to input the ID of the group whose members you want to fetch.
  • Fetching Members: Retrieves members of the specified group, loop through each of them, and displays their DisplayName, Email and Job Title properties on the PowerShell console.
  • Disconnects from Graph PowerShell Module: Since it's good practice to disconnect from the session once your operations are complete, the script uses Disconnect-MgGraph cmdlet to disconnect the session.

Further Enhancing the Script

Here's how you can enhance the functionality and usability of this Graph PowerShell script that fetches Microsoft 365 group members:

  • Error Handling:Introduce error handling to manage common issues like incorrect group IDs, connectivity problems, or insufficient permissions. This would make the script more reliable and easier to troubleshoot.
  • Enhancing Graph PowerShell Script for listing Microsoft 365 Group Owners with filtering.
  • Filtering and Sorting: Allow users to filter or sort the output based on various attributes such as job title, department, or location. This could be particularly useful for large groups.
  • Enhancing Graph PowerShell Script for listing Microsoft 365 Group Owners by outputting results to file.
  • Enhanced Output Formatting:Improve the readability of the output by using formatted tables or custom color highlighting for different types of output, making it easier to scan through results.
  • Enhancing Graph PowerShell Script for listing Microsoft 365 Group Owners with progress indicators.
  • Interactive Selection of Groups: Instead of requiring the group ID as input, the script could list all available groups and allow the user to select one interactively. This is user-friendly, especially for those who may not know the Group ID offhand.
  • Enhancing Graph PowerShell Script for listing Microsoft 365 Group Owners with parameterizing.
  • Dynamic Property Selection: Allow the user to specify which properties to fetch and display, making the script adaptable to different needs without modifying the code.
  • Enhancing Graph PowerShell Script for listing Microsoft 365 Group Owners with additional owner details.

Each of these enhancements targets a different aspect of the script's usability, robustness, or functionality, contributing to a more comprehensive and flexible tool for managing Microsoft 365 group memberships.

Frequently Asked Questions

1. How can I retrieve the members of a specific Microsoft 365 group?

Use the following command to list the members of a group:

Get-MgGroupMember -GroupId "<GroupId7>" -All

2. How can I filter group members by role (e.g., owner or member)?

You can use the OdataType property to filter roles. Example:

$Members = Get-MgGroupMember -GroupId "<GroupId>" -All
$Owners = $Members | Where-Object { $_.OdataType -eq '#microsoft.graph.groupOwner' }
$MembersOnly = $Members | Where-Object { $_.OdataType -eq '#microsoft.graph.groupMember' }
                            

3. Can I retrieve members for all groups in my tenant?

Yes, loop through all groups to retrieve their members. Example:

$Groups = Get-MgGroup -All
foreach ($Group in $Groups) {
    $Members = Get-MgGroupMember -GroupId $Group.Id -All
    Write-Output "Group: $($Group.DisplayName)"
    Write-Output $Members
}

4. How can I export group members to a CSV file?

Use this script to export group members for a specific group:

$Members = Get-MgGroupMember -GroupId "<GroupId>" -All
$Members | Select-Object DisplayName, UserPrincipalName, Id | Export-Csv -Path "C:\Path\To\GroupMembers.csv" -NoTypeInformation              

5. What permissions are required to retrieve group members?

You need the GroupMember.Read.All or GroupMember.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure appropriate permissions are granted in Azure AD.


Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell
Import M365 Users to Microsoft Teams from CSV file
Get Microsoft 365 Group Owners List Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex