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.
Install-Module Microsoft.Graph -Scope CurrentUser.Connect-MgGraph -Scopes "TeamMember.Read.All".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.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:
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].Get-MgTeamMember cmdlet is used to retrieve all members of the specified team using the -All parameter.foreach loop iterates over each team member. The AdditionalProperties property of each member is accessed to retrieve the userId.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.Format-Table cmdlet with the -AutoSize parameter for better readability.
$Members = Get-MgTeamMember -TeamId "<TeamId>"
$Members | Select-Object DisplayName, Roles, Email | Export-Csv -Path "C:\Path\To\TeamMembers.csv" -NoTypeInformation
TeamId to Retrieve MembersGet-MgTeamMember cmdlet requires a valid TeamId to function.Get-MgTeam or by identifying the associated Microsoft 365 group via Get-MgGroup.roles Property to Differentiate Owners from MembersGet-MgTeamMember includes a roles property.roles contains "owner", the user is a Team owner.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.
© m365corner.com. All Rights Reserved. Design by HTML Codex