Managing Microsoft Teams within an organization can become challenging as the number of teams grows over time. One important task for IT administrators is identifying and managing archived teams—teams that are no longer active but need to be retained for reference or compliance reasons. In this article, we'll walk you through a PowerShell script that uses the Microsoft Graph API to list all archived teams in your tenant, providing essential details like the team name, description, type, and archive status.
# Ensure the Microsoft.Graph module is installed and imported
if (-not (Get-Module -Name Microsoft.Graph)) {
Install-Module -Name Microsoft.Graph -Force -AllowClobber
}
Import-Module Microsoft.Graph
# Authenticate to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All"
# Get all groups associated with Teams in the tenant
$teamsGroups = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -All
# Initialize an array to store results
$results = @()
foreach ($group in $teamsGroups) {
try {
# Fetch the team details
$team = Get-MgTeam -TeamId $group.Id
}
catch {
# If team details cannot be fetched, skip to the next group
continue
}
# Check if the team is archived
if ($team.IsArchived -eq $true) {
# Determine the team type (private or public)
$teamType = if ($group.Visibility -eq "Private") { "Private" } else { "Public" }
# Create a hashtable for each archived team's data
$teamData = @{
"Team Name" = $group.DisplayName
"Description" = $group.Description
"Team Type" = $teamType
"Archive Status" = "Enabled"
}
# Add the hashtable to the results array
$results += New-Object PSObject -Property $teamData
}
}
# Output the results in a table format
$results | Format-Table -AutoSize
This script leverages the Microsoft Graph API via PowerShell to query and list archived teams within a Microsoft 365 tenant. Here’s a breakdown of how the script operates:
This script provides a strong foundation for managing archived teams, but it can be further enhanced to meet specific organizational needs:
$results | Export-Csv -Path "ArchivedTeamsReport.csv" -NoTypeInformation
Error: "Insufficient privileges to complete the operation."
Cause: The authenticated account does not have the required permissions.
Solution: Ensure that the account used has at least the Group.Read.All permission in Microsoft Graph.
Error: "404 page not found."
Cause: This error may occur if the team cannot be found, possibly due to deletion or an incorrect parameter.
Solution: The script handles this by using a try-catch block to continue processing other teams without failing. Ensure that the GroupId is correct and the team exists.
Error: "HTTP 429 Too Many Requests."
Cause: The script may hit Microsoft Graph API rate limits if there are too many requests in a short period.
Solution: Implement retry logic or reduce the frequency of API calls.
This PowerShell script offers a practical way to manage and report on archived Microsoft Teams within your organization. By automating the process of identifying archived teams, you can keep your Teams environment organized and compliant with your organization’s data retention policies. With the ability to further enhance and customize the script, you can tailor it to meet the specific needs of your administrative tasks.
© m365corner.com. All Rights Reserved. Design by HTML Codex