Listing the Top Microsoft Teams by Member Count

Managing Microsoft Teams can be a challenging task, especially in large organizations with numerous teams. Knowing which teams are the most active based on member count can help administrators prioritize resources and support. In this article, we’ll explore how to use Microsoft Graph PowerShell to list Microsoft Teams in your tenant sorted by their member count. The script provided will help you identify the most populous teams, making it easier to manage and support them.


The Script

# Ensure the Microsoft Graph PowerShell module is installed
# Install-Module Microsoft.Graph -Scope CurrentUser -Force

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

# Get all Microsoft Teams
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -All -Property Id, DisplayName

# Initialize an array to store team and member count
$teamMemberCounts = @()

# Loop through each team and get the member count
foreach ($team in $teams) {
    $members = Get-MgGroupMember -GroupId $team.Id -All
    $teamMemberCounts += [PSCustomObject]@{
        "Team Name"    = $team.DisplayName
        "Member Count" = $members.Count
    }
}

# Sort the teams by MemberCount in descending order
$sortedTeams = $teamMemberCounts | Sort-Object "Member Count" -Descending

# Output the sorted teams with their member counts
$sortedTeams | Format-Table -AutoSize

# Disconnect from Microsoft Graph
Disconnect-MgGraph

How the Script Works

This script leverages the Microsoft Graph PowerShell module to retrieve and sort Microsoft Teams based on the number of members in each team. Below is a step-by-step breakdown of how the script works:

  1. Connecting to Microsoft Graph: The script begins by establishing a connection to Microsoft Graph using the Connect-MgGraph cmdlet. This requires the Group.Read.All permission, which allows the script to read group information.
  2. Retrieving Teams: The script then fetches all groups in the tenant that are provisioned as Microsoft Teams. This is done using the Get-MgGroup cmdlet with a filter applied to retrieve only those groups that have 'Team' as part of their resourceProvisioningOptions.
  3. Counting Members: For each team retrieved, the script uses the Get-MgGroupMember cmdlet to list all members of the team. The number of members is then stored in an array along with the team's name.
  4. Sorting the Teams: The array containing the team names and their respective member counts is sorted in descending order by the member count, ensuring that the team with the highest number of members appears at the top.
  5. Outputting Results: Finally, the script outputs the sorted list of teams along with their member counts in a table format.
  6. Disconnecting from Microsoft Graph: After completing the task, the script disconnects from the Microsoft Graph session to free up resources.

Further Enhancements

This script provides a solid foundation for listing teams based on member count, but there are several enhancements you could consider:

  • Filter by Date: Modify the script to include only active members within a certain time frame, such as the last 30 days, to focus on more active teams.
  • Exporting Results: Add functionality to export the results to a CSV or Excel file for further analysis or reporting.
  • Include Additional Data: Enhance the script to include additional information, such as the team's creation date or the last activity date, to gain more insights.
  • Automate the Process: Set up a scheduled task to run this script periodically and send the results via email to stakeholders.

Possible Errors & Solutions

Permission Issues:

Error: Insufficient privileges to complete the operation.

Cause: The account used to run the script lacks the required permissions.

Solution: Ensure that the account has the Group.Read.All permission. If not, assign the necessary permissions.

API Rate Limits:

Error: Rate limit exceeded.

Cause: Too many requests made to the Microsoft Graph API in a short period.

Solution: Introduce a delay between requests or handle rate limit errors with a retry logic in the script.

Empty Member Count:

Error: Member count returns as zero.

Cause: The Get-MgGroupMember cmdlet might not be returning results due to API issues.

Solution: Verify that the teams actually have members and ensure there are no connectivity issues. If the problem persists, try re-running the script after a few minutes.


Conclusion

The script provided in this article offers a simple yet powerful way to list and sort Microsoft Teams in your tenant based on member count. By using Microsoft Graph PowerShell, you can easily gather and analyze this data, helping you make informed decisions about team management. With the additional enhancements and error handling, you can further tailor the script to meet your specific needs. This is just one of the many ways Microsoft Graph can streamline administrative tasks in your organization.

If you find this script useful, consider incorporating it into your regular management tasks or enhancing it to provide even more valuable insights.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

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