How to List Archived Microsoft Teams Using Graph PowerShell

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.


The Script: Listing Archived Teams

# 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

How the Script Works

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:

  • Module Installation and Import: The script begins by checking if the Microsoft.Graph module is installed. If it's not, the module is installed and imported. This module is necessary for interacting with the Microsoft Graph API through PowerShell.
  • Authentication: The script uses Connect-MgGraph to authenticate to Microsoft Graph with the necessary Group.Read.All scope to read group and team information.
  • Retrieve Teams-Associated Groups: The script retrieves all Microsoft 365 groups that have been provisioned as Teams using Get-MgGroup with a filter.
  • Try-Catch Block for Error Handling: A try-catch block attempts to retrieve the details of each team using the Get-MgTeam cmdlet. If this fails (for instance, if the team no longer exists or if there’s a permission issue), the script gracefully skips to the next group without terminating.
  • Filter Archived Teams: The script checks each team’s IsArchived property to identify whether the team is archived.
  • Output: Finally, the script outputs the archived teams in a tabular format displaying the team name, description, type (private or public), and archive status.

Further Enhancements

This script provides a strong foundation for managing archived teams, but it can be further enhanced to meet specific organizational needs:

  • Exporting Results to CSV: To store the results for reporting or further analysis, you can export the output to a CSV file by adding the following line at the end of the script:
    $results | Export-Csv -Path "ArchivedTeamsReport.csv" -NoTypeInformation
  • Automating Alerts for Archived Teams: You can set up this script to run periodically and send an email alert if any new teams are archived. This can be achieved by integrating the script with an automation tool like Azure Automation or scheduling it with Task Scheduler.
  • Filtering by Date: To identify teams that have been archived within a specific timeframe, you could enhance the script to include date filtering based on when the team was archived.

Possible Errors & Solutions

"Authentication Issues"

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.

"404 Errors"

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.

"Rate Limits"

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.


Conclusion

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.


Related Articles:

Listing Guest Users in a Microsoft Team Using Graph PowerShell
How to Check for Password Expired Users in Microsoft 365
How to Check for Sign-In Enabled Users in Microsoft 365
How to Remove Users from a Microsoft 365 Group Using PowerShell
Counting Groups in Your Microsoft 365 Tenant
How to Retrieve the Last User Sign-In Date Using Graph PowerShell
How to Get a List of Archived Teams in Microsoft 365 Using Graph PowerShell
Monitoring Guest User Invitations in Microsoft 365 Using Graph PowerShell
How to Retrieve Teams Channel Details in Microsoft 365 Using Graph PowerShell

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