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.
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
To make this script even more powerful, you can consider the following enhancements:
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" |
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex