Knowing who belongs to a Microsoft 365 group is a daily admin need. Whether you’re auditing access, troubleshooting membership issues, or preparing reports, the Get-MgGroupMember cmdlet in Microsoft Graph PowerShell gives you a fast way to retrieve and analyze group members.
In this article, you’ll learn 5 simple and practical scripts that show different ways to work with Get-MgGroupMember. Each example is beginner-friendly and useful in real admin scenarios.
Before using these scripts, install and connect Microsoft Graph PowerShell with the right permissions:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Group.Read.All","User.Read.All"
You’ll also need the GroupId of the group you want to query. You can find one using:
Get-MgGroup -Filter "displayName eq 'Group Name'"
Retrieve all members of a specified group.
Get-MgGroupMember -GroupId "<GroupId>"
Explanation:
This is the most basic way to list members. It returns every object inside the group — usually users, but sometimes devices or service principals too. Great for quickly verifying membership.
Search for group members using a specific search string.
Get-MgGroupMember -GroupId "<GroupId>" -Search "displayName:John" -ConsistencyLevel eventual
Explanation:
This script searches within the group’s members.
Useful when you want to confirm whether a specific person is part of a group.
Retrieve the top N members of the group.
Get-MgGroupMember -GroupId "<GroupId>" -Top 10
Explanation:
This retrieves only the first 10 members. It’s handy when testing scripts or when you don’t need the full membership list (especially for large groups).
Sort the group members based on a specific property.
Get-MgGroupMember -GroupId "<GroupId>" -Sort "displayName asc" -ConsistencyLevel eventual -CountVariable groupCount
Explanation:
This script returns members sorted alphabetically by displayName.
Great for generating clean admin reports.
Retrieve the group members and then fetch their displayName and userPrincipalName with the help of Get-MgUser cmdlet:
# Retrieve members of a specified group
$groupMembers = Get-MgGroupMember -GroupId "1cbe8c31-589d-453a-a1e5-045f7f00c967"
# Initialize an array to store detailed user information
$userDetails = @()
# Loop through each group member and retrieve additional properties
foreach ($member in $groupMembers) {
$user = Get-MgUser -UserId $member.Id -Property "id, displayName, userPrincipalName"
$userDetails += [PSCustomObject]@{
Id = $user.Id
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
}
}
# Display the detailed user information
$userDetails | Select-Object Id, DisplayName, UserPrincipalName
Explanation:
Get-MgGroupMember usually returns only limited information.
This script:
This is one of the most useful real-world reporting patterns for admins.
$userDetails | Export-Csv C:\Reports\GroupMembers.csv -NoTypeInformation
| Error | Cause | Solution |
|---|---|---|
| Insufficient privileges to complete the operation | Missing Graph scopes | Reconnect with "Group.Read.All","User.Read.All" |
| Request_UnsupportedQuery | Search/sort without consistency level | Add -ConsistencyLevel eventual |
| Group not found | Incorrect GroupId | Verify using Get-MgGroup |
| Cannot index into a null array | Empty group | Check if group has members or handle null safely |
The Get-MgGroupMember cmdlet is a must-know tool for Microsoft 365 administrators. Whether you're listing members, searching within them, sorting results, limiting output for testing, or building detailed reports — these five scripts cover the most practical day-to-day use cases.
Once you're comfortable with these patterns, you can easily extend them into automated audits, compliance checks, or scheduled membership reports.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex