Listing the Top Microsoft Teams by Owner Count

In Microsoft Teams management, understanding which teams have the most owners can be crucial for ensuring proper governance and oversight. Teams with multiple owners might be easier to manage but could also lead to challenges in coordination and decision-making. This article provides a PowerShell script that lists the top Microsoft Teams in your tenant based on the owner count, with the team having the highest number of owners appearing at the top. This information can help administrators identify teams that may require closer management or review.


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 owner count
$teamOwnerCounts = @()

# Loop through each team and get the owner count
foreach ($team in $teams) {
    $owners = Get-MgGroupOwner -GroupId $team.Id -All
    $teamOwnerCounts += [PSCustomObject]@{
        "Team Name"    = $team.DisplayName
        "Owner Count"  = $owners.Count
    }
}

# Sort the teams by Owner Count in descending order
$sortedTeams = $teamOwnerCounts | Sort-Object "Owner Count" -Descending

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

# Disconnect from Microsoft Graph
Disconnect-MgGraph

How the Script Works

This script efficiently retrieves and sorts Microsoft Teams based on the number of owners each team has. Below is a step-by-step explanation of how the script operates:

  1. Connecting to Microsoft Graph: The script begins by connecting to Microsoft Graph using the Connect-MgGraph cmdlet. You must have the Group.Read.All permission granted to the user account executing this script as it allows the script to read group information across the tenant.
  2. Retrieving Teams: Using the Get-MgGroup cmdlet, the script filters and retrieves only those groups that are provisioned as Microsoft Teams. The script selects the Id and DisplayName properties for further processing.
  3. Counting Owners: For each team retrieved, the script uses the Get-MgGroupOwner cmdlet to list all owners of the team. It then counts the number of owners and stores this information along with the team name in an array.
  4. Sorting the Teams: The array is then sorted by the owner count in descending order, ensuring that the team with the highest number of owners appears at the top of the list.
  5. Outputting Results: Finally, the sorted list of teams and their owner counts are displayed in a table format using Format-Table.
  6. Disconnecting from Microsoft Graph: After completing the task, the script disconnects from Microsoft Graph to free up any resources used during the session.

Further Enhancements

The provided script serves as a solid base for listing teams by owner count. However, you can enhance it further to meet specific needs:

  • Filter by Specific Teams: Modify the script to focus on specific teams, such as those belonging to a particular department or business unit, by adding appropriate filtering criteria.
  • Export Results: Add functionality to export the sorted list of teams and their owner counts to a CSV or Excel file for reporting or further analysis.
  • Include Additional Information: Enhance the script to include additional details, such as the email addresses of the owners, team creation dates, or team activity levels.
  • Automate the Process: Automate the execution of this script on a scheduled basis and send the results via email to stakeholders or store them in a SharePoint document library.

Possible Errors & Solutions

Permission Issues:

Error: Insufficient privileges to complete the operation.

Cause: The user account does not have the required permissions to read group information.

Solution: Ensure the account has the Group.Read.All permission in Microsoft Graph. You may need to ask an administrator to grant this permission.

API Rate Limits:

Error: Rate limit exceeded.

Cause: The script makes too many requests to the Microsoft Graph API in a short period.

Solution: Introduce a delay between requests or implement retry logic in the script to handle rate limits gracefully.

Empty Owner Count:

Error: The owner count returns as zero.

Cause: The Get-MgGroupOwner cmdlet might not be returning results due to connectivity issues or because the group genuinely has no owners.

Solution: Ensure that the teams in question have owners and that the script is running without connectivity issues. If the problem persists, try re-running the script or manually verifying the team owner count through the Teams admin center.


Conclusion

This PowerShell script provides an effective way to list and sort Microsoft Teams by the number of owners, giving you insight into team ownership within your organization. By identifying teams with unusually high or low owner counts, you can ensure that governance policies are being followed and make informed decisions about team management.

The script is also a starting point for further customization, allowing you to tailor it to your specific needs. Whether you need to focus on particular teams, export the results, or automate the process, this script can be extended to fit those requirements.

If you found this script helpful, consider incorporating it into your regular team management processes or further enhance it to provide more detailed insights into your Microsoft Teams environment.


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