Listing the Count of Public, Private, and Org-wide Teams using Graph PowerShell

Microsoft Teams offers different types of teams within an organization: public, private, and org-wide. Understanding the distribution of these teams can help administrators manage their tenant more effectively. This article will guide you through a PowerShell script using Microsoft Graph to identify and count the number of each type of team within your Microsoft 365 tenant. We'll explain how the script works, suggest enhancements, cover possible errors, and provide solutions.


Script to Identify and Count Team Types

# Ensure you have the Microsoft.Graph module installed
Install-Module Microsoft.Graph -Scope CurrentUser -Force -AllowClobber

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

# Get all teams in the tenant
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -All

# Initialize counters
$publicTeamsCount = 0
$privateTeamsCount = 0
$orgWideTeamsCount = 0

# Iterate through each team and count based on visibility
foreach ($team in $teams) {
    $teamVisibility = $team.Visibility
    if ($teamVisibility -eq "Public") {
        $publicTeamsCount++
    } elseif ($teamVisibility -eq "Private") {
        $privateTeamsCount++
    } elseif ($teamVisibility -eq $null -and $team.Description -like "*org-wide*") {
        $orgWideTeamsCount++
    }
}

# Output the counts
Write-Output "Public Teams: $publicTeamsCount"
Write-Output "Private Teams: $privateTeamsCount"
Write-Output "Org-wide Teams: $orgWideTeamsCount"

Script Output:


How the Script Works

  1. Install-Module: Ensures the Microsoft.Graph module is installed if it's not already.
  2. Connect-MgGraph: Authenticates and connects to Microsoft Graph using the specified scopes.
  3. Get-MgGroup: Retrieves all groups with resourceProvisioningOptions set to 'Team', indicating we only want Microsoft Teams enabled groups.
  4. Initialize Counters: Sets up counters for each type of team (public, private, and org-wide).
  5. Iterate and Count: Loops through each team and increments the appropriate counter based on the team's visibility. Org-wide teams are identified by checking if the Visibility property is null and if the description contains "org-wide".
  6. Output: Displays the count of public, private, and org-wide teams.
🔍 The visibility Property Helps Differentiate Public, Private, and Org-Wide Teams

When querying Microsoft Teams using Graph PowerShell, use the visibility property to identify:
  • Public Teams – visible to everyone
  • Private Teams – membership-controlled
  • Org-Wide Teams – marked as HiddenMembership
This property is key to accurately classifying teams.

💡 Filter Groups by resourceProvisioningOptions to Target Teams Only

Not all Microsoft 365 groups have an associated Microsoft Team.

To avoid counting security or distribution groups, filter for groups where resourceProvisioningOptions includes "Team". This ensures you're only counting active Teams.

Enhancements to the Script

The script can be further enhanced in several ways:

  • Export to CSV: Export the counts to a CSV file for easier reporting and record-keeping.
  • # Export counts to CSV
    $output = [PSCustomObject]@{
        PublicTeamsCount = $publicTeamsCount
        PrivateTeamsCount = $privateTeamsCount
        OrgWideTeamsCount = $orgWideTeamsCount
    }
    $output | Export-Csv -Path "TeamsCount.csv" -NoTypeInformation
  • Detailed Logging: Add detailed logging to capture the script's output and any errors for troubleshooting and auditing.
  • # Log the output
    $logFile = "TeamsCountLog.txt"
    $output | Out-File -FilePath $logFile
  • Additional Filtering: Include additional filters to refine the team selection based on other properties such as creation date or owner.
  • # Filter teams created in the last year
    $lastYear = (Get-Date).AddYears(-1)
    $recentTeams = $teams | Where-Object { $_.CreatedDateTime -gt $lastYear }

Possible Errors and Solutions

Resource Not Found (404):

Error Message: "Get-MgGroup : Resource 'xxxxxx' does not exist or one of its queried reference-property objects are not present. Status: 404 (NotFound) ErrorCode: Request_ResourceNotFound"

Solution: Verify the group ID. Ensure it is correct and corresponds to an existing Microsoft Team.

Permissions Issue:

Error Message: "Insufficient privileges to complete the operation."

Solution: Ensure you have the required permissions. The script needs Group.Read.All permissions. Re-authenticate with the correct scopes if necessary.

Connection Issues:

Error Message: "Connect-MgGraph : AADSTS700016: Application with identifier 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' was not found in the directory 'yourdirectory.onmicrosoft.com'."

Solution: Ensure you are connecting to the correct tenant. Verify the application registration in Azure AD and its permissions.


Conclusion

Using Graph PowerShell to manage and query team types in Microsoft Teams provides a powerful way to maintain control and ensure effective team management. The provided script allows you to identify and count public, private, and org-wide teams efficiently. With the suggested enhancements, you can tailor the script to better suit your needs. Understanding possible errors and their solutions ensures smooth operation and troubleshooting.

For more advanced management and automation tasks, consider exploring additional Microsoft Graph PowerShell cmdlets and incorporating them into your workflows.


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