Using Get-MgTeamMember in Graph PowerShell

This guide demonstrates how to use the Get-MgTeamMember cmdlet in Microsoft Graph PowerShell to retrieve members of Microsoft Teams. Learn how to list team members and export them with practical examples.

The Get-MgTeamMember cmdlet is a part of the Microsoft Graph PowerShell module that allows administrators to retrieve information about members of a Microsoft Teams team. This cmdlet is particularly useful for managing and auditing team memberships within an organization.


Prerequisites

  • You need to be a Teams Administrator.
  • Install the Graph PowerShell module by running the command: Install-Module Microsoft.Graph -Scope CurrentUser.
  • Connect to Graph PowerShell with the scope ‘TeamMember.Read.All' by running the command: Connect-MgGraph -Scopes "TeamMember.Read.All".

Cmdlet Syntax

Get-MgTeamMember -TeamId <String> [-All] []
  • -TeamId <String>: Specifies the ID of the team whose members are to be retrieved.
  • -All: Retrieves all team members.
  • [CommonParameters]: Supports common parameters such as -Verbose, -Debug, etc.

Example Script

This script uses Get-MgTeamMember cmdlet and fetches all the team members. Then uses Get-MgUser cmdlet to get detailed info about the fetched users.

$teamId = "9f47ec97-db44-41db-8867-3793cff0a49a"

# Get all team members
$teamMembers = Get-MgTeamMember -TeamId $teamId -All

# Retrieve detailed information for each team member
$teamMemberDetails = foreach ($member in $teamMembers) {
    $additionalProps = $member.AdditionalProperties
    $userId = $additionalProps["userId"]
    if ($userId -ne $null -and $userId -ne "") {
        $user = Get-MgUser -UserId $userId
        [PSCustomObject]@{
            Id = $user.Id
            DisplayName = $user.DisplayName
            UserPrincipalName = $user.UserPrincipalName
        }
    }
}

# Display the team member details
$teamMemberDetails | Format-Table -AutoSize

The script output should be as follows:


Script Explanation


  • Define Team ID: The script begins by defining the teamId variable with the ID of the team you want to query. [Use Get-MgTeam cmdlet to get the Team ID and then add it to the script].
  • Retrieve Team Members: The Get-MgTeamMember cmdlet is used to retrieve all members of the specified team using the -All parameter.
  • Iterate Over Team Members: A foreach loop iterates over each team member. The AdditionalProperties property of each member is accessed to retrieve the userId.
  • Get User Details: If the userId is not null or empty, the Get-MgUser cmdlet retrieves the user's details. These details are then stored in a custom PowerShell object containing the Id, DisplayName, and UserPrincipalName of the user.
  • Display Results: Finally, the details of all team members are displayed in a formatted table using the Format-Table cmdlet with the -AutoSize parameter for better readability.

Script Improvements


  • Error Handling: Add error handling to manage cases where a user ID might not be found or other issues arise.
  • Parallel Processing: Use parallel processing to improve performance when retrieving large numbers of team members.
  • Additional Properties: Retrieve and display more properties of the team members if needed.

Frequently Asked Questions

  • What is Get-MgTeamMember used for?
    Get-MgTeamMember is a Microsoft Graph PowerShell cmdlet used to retrieve details about members of Microsoft Teams, including their display names, roles, and user IDs.
  • How can I export team member details to a CSV file?
    Use this script to export member details like display name, role, and email:
    $Members = Get-MgTeamMember -TeamId "<TeamId>"
    $Members | Select-Object DisplayName, Roles, Email | Export-Csv -Path "C:\Path\To\TeamMembers.csv" -NoTypeInformation
  • What permissions are required to retrieve team members?
    You need the TeamMember.Read.All or TeamMember.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure these permissions are granted in Azure AD.
You Must Provide the Correct TeamId to Retrieve Members

The Get-MgTeamMember cmdlet requires a valid TeamId to function.

You can obtain this ID by running Get-MgTeam or by identifying the associated Microsoft 365 group via Get-MgGroup.

An incorrect or deleted Team ID will result in an empty result or a 404 error.
Check the roles Property to Differentiate Owners from Members

Each user returned by Get-MgTeamMember includes a roles property.

- If roles contains "owner", the user is a Team owner.
- If it’s empty, the user is a regular member.

This is especially useful for audits and reporting on team ownership distribution.

Conclusion

The Get-MgTeamMember cmdlet is a powerful tool for managing Microsoft Teams memberships. By leveraging this cmdlet, administrators can easily retrieve detailed information about team members and automate many aspects of team management. The provided script demonstrates how to use the cmdlet effectively and offers a foundation for further customization and improvements.

Suggested Reading

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