Fetch Microsoft Teams Members Using Graph PowerShell

Managing Teams in Microsoft 365 requires administrators to have insights into team membership. This PowerShell script leverages Microsoft Graph API to fetch and display members of a specified Team, using either the Team ID or Display Name as input. This script is particularly useful for IT admins who need to audit Team memberships or manage Teams effectively.


The Script

Below is the PowerShell script that retrieves and displays Team members:

# Ensure Microsoft Graph module is installed and imported
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Teams)) {
    Install-Module Microsoft.Graph -Force -Scope CurrentUser
}
Import-Module Microsoft.Graph
                                
# Connect to Microsoft Graph with the necessary permissions
Connect-MgGraph -Scopes "Group.Read.All", "TeamMember.Read.All"
                                
# Prompt user for Team ID or Display Name
$TeamIdentifier = Read-Host "Enter the Team ID or Display Name"
                                
# Retrieve the Team based on ID or Display Name
$Team = Get-MgTeam -All -Property Id, DisplayName | Where-Object { $_.Id -eq $TeamIdentifier -or $_.DisplayName -eq $TeamIdentifier }
                                
# Check if Team exists
if (-not $Team) {
    Write-Host "No matching team found. Please check the Team ID or Display Name and try again." -ForegroundColor Red
    exit
}
                                
# Retrieve all members of the specified team
$Members = Get-MgTeamMember -TeamId $Team.Id -All
                                
Write-Host "`nMembers of Team: $($Team.DisplayName)`n" -ForegroundColor Cyan

# Check if the team has members
if ($Members.Count -eq 0) {
    Write-Host "No members found in this team." -ForegroundColor Yellow
    exit
}
                                
# Display members in a table format
$Members | Select-Object @{Name="Team Name"; Expression={$Team.DisplayName}}, DisplayName | Format-Table -AutoSize
                                
# Disconnect from Microsoft Graph
Disconnect-MgGraph
                                
                                

How the Script Works

  1. Connects to Microsoft Graph: The script ensures that the Microsoft Graph module is installed and imports it before establishing a connection to Microsoft Graph with Group.Read.All and TeamMember.Read.All permissions.
  2. Prompts the user: The script requests input from the user, either a Team ID or a Team Display Name.
  3. Fetches the team information: It queries all Teams and filters them based on the provided ID or Display Name.
  4. Validates the Team existence: If the specified team is not found, the script exits with an error message.
  5. Retrieves all Team Members: It fetches all members of the specified team using the Get-MgTeamMember cmdlet.
  6. Displays the results in a table format: Once the process is complete, the script disconnects from the Graph session.

Further Enhancements

To make this script even more powerful, you can consider the following enhancements:

  • Include Email and UPN: Modify the script to fetch the email addresses and User Principal Names (UPN) of the team members.
  • $Members | Select-Object @{Name="Team Name"; Expression={$Team.DisplayName}}, DisplayName, Mail, UserId | Format-Table -AutoSize
  • Export to CSV: Add a command to save the output to a CSV file for auditing purposes.
  • $Members | Export-Csv -Path "C:\TeamsMembers.csv" -NoTypeInformation
  • Allow Selection from Multiple Teams: If multiple teams match the provided name, display them in a numbered list and let the user choose.

Possible Errors & Solutions

Error Cause Solution
No matching team found Ensure the correct Team Display Name or ID is used. You can retrieve available teams using: Get-MgTeam -All | Select DisplayName, Id
No members found in this team The specified team has no members or permissions are insufficient. Verify team membership in Microsoft Teams Admin Center and ensure you have the correct Graph API permissions.
Insufficient privileges to access this data Ensure the necessary permissions are granted. Reconnect using: Connect-MgGraph -Scopes "Group.Read.All", "TeamMember.Read.All"

Conclusion

This PowerShell script is a simple yet powerful way to retrieve and display Microsoft Teams members using Microsoft Graph. It provides a quick audit of team memberships, helping administrators manage Teams effectively.

By making slight modifications, such as exporting to CSV or including additional user details, this script can be adapted for more complex use cases.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

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