Fetch Microsoft Team Member Count Using Graph PowerShell

Managing Microsoft Teams in a large organization often requires generating reports that provide insights into the structure and size of various teams. One such report might involve listing the total number of members for each team within the tenant, including details like the team name and whether the team is private or public. In this article, we'll walk you through a simple Graph PowerShell script that automates this task, making it easier for IT administrators to manage and report on their Teams environment.


The Script: Listing Team Members Count, Team Name, and Team Type

# Ensure the Microsoft.Graph module is installed and imported
if (-not (Get-Module -Name Microsoft.Graph.Teams)) {
    Install-Module -Name Microsoft.Graph.Teams -Force -AllowClobber
}
Import-Module Microsoft.Graph.Teams

# Authenticate to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All"

# Get all Teams in the tenant
$teams = Get-MgTeam -All

# Initialize an array to store results
$results = @()

foreach ($team in $teams) {
    # Get the member count for each team
    $memberCount = (Get-MgTeamMember -TeamId $team.Id -All).Count

    # Determine the team type (private or public)
    $teamType = if ($team.Visibility -eq "Private") { "Private" } else { "Public" }

    # Create a hashtable for each team's data
    $teamData = @{
        "Team Name"  = $team.DisplayName
        "Member Count" = $memberCount
        "Team Type"  = $teamType
    }

    # Add the hashtable to the results array
    $results += New-Object PSObject -Property $teamData
}

# Output the results in a table format
$results | Format-Table -AutoSize

How the Script Works

This script is designed to be straightforward, ensuring that even administrators with basic PowerShell knowledge can use it effectively. Here's a breakdown of how the script works:

  • Module Installation and Import: The script first checks if the Microsoft.Graph.Teams module is installed. If not, it installs the module and then imports it. This module is essential for interacting with Microsoft Teams via the Microsoft Graph API.
  • Authentication: The script then authenticates to Microsoft Graph using Connect-MgGraph with the necessary Group.Read.All scope. This scope allows the script to read all group information, which is required to list teams and their members.
  • Fetching Teams: The Get-MgTeam -All cmdlet retrieves all Teams within the tenant. The -All parameter ensures that all teams are retrieved, even if there are many.
  • Looping Through Teams: The script loops through each team, retrieving the member count using Get-MgTeamMember. It also checks the Visibility property to determine whether the team is private or public.
  • Output: The script stores the team's name, member count, and type in a hashtable, which is then output in a table format using Format-Table, making the data easy to read and understand.

Further Enhancing the Script

  • Exporting to CSV: You might want to export the results to a CSV file for further analysis or archiving. You can easily add the following line at the end of the script:
  • $results | Export-Csv -Path "TeamsMemberCountReport.csv" -NoTypeInformation
  • Filtering Teams: If you're only interested in specific teams, you can modify the script to filter teams based on their name or type. For example, to list only private teams, you could add a condition within the loop:
  • if ($team.Visibility -eq "Private") {
        # Your existing code
    }
  • Including Owner Information: The script can be enhanced to include the names of team owners. This would require an additional call to Get-MgTeamMember with the role of 'Owner'.

Possible Errors & Solutions

Authentication Issues

Error: "Insufficient privileges to complete the operation."

Cause: The authenticated account does not have the required permissions.

Solution: Ensure that the account you are using has at least the Group.Read.All permission in Microsoft Graph.

Module Not Found

Error: "The term 'Get-MgTeam' is not recognized as the name of a cmdlet."

Cause: The Microsoft.Graph.Teams module is not installed or imported correctly.

Solution: Make sure the module is installed using Install-Module -Name Microsoft.Graph.Teams and imported using Import-Module Microsoft.Graph.Teams.

Rate Limits

Error: "HTTP 429 Too Many Requests."

Cause: The script is hitting Microsoft Graph API rate limits.

Solution: Implement a retry logic with a delay in your script or reduce the frequency of requests to the Graph API.


Conclusion

This Graph PowerShell script offers a simple yet powerful way to manage and report on Microsoft Teams within your organization. By automating the process of retrieving member counts, team names, and team types, administrators can save time and reduce errors associated with manual reporting. Additionally, with a few enhancements, the script can be tailored to meet more specific needs, such as exporting data, filtering results, or including additional details like team owners.

By leveraging the power of Graph PowerShell, you can take control of your Microsoft 365 environment, streamline administrative tasks, and ensure that your teams are managed efficiently. If you encounter any issues, refer to the possible errors and solutions section to troubleshoot common problems.


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