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.
# 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:
visibility Property Helps Differentiate Public, Private, and Org-Wide Teamsvisibility property to identify:
HiddenMembershipresourceProvisioningOptions to Target Teams OnlyresourceProvisioningOptions includes "Team".
This ensures you're only counting active Teams.
The script can be further enhanced in several ways:
# Export counts to CSV
$output = [PSCustomObject]@{
PublicTeamsCount = $publicTeamsCount
PrivateTeamsCount = $privateTeamsCount
OrgWideTeamsCount = $orgWideTeamsCount
}
$output | Export-Csv -Path "TeamsCount.csv" -NoTypeInformation
# Log the output
$logFile = "TeamsCountLog.txt"
$output | Out-File -FilePath $logFile
# Filter teams created in the last year
$lastYear = (Get-Date).AddYears(-1)
$recentTeams = $teams | Where-Object { $_.CreatedDateTime -gt $lastYear }
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.
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex