Retrieve Archived Microsoft Teams Using Graph PowerShell

Microsoft Teams is an essential collaboration tool for organizations, but as projects end or Teams become inactive, administrators may need to archive Teams to preserve their data. Archiving ensures the content remains accessible while preventing further activity.

In this article, we'll introduce a Graph PowerShell script that retrieves all archived Teams and displays them in a tabular format, making it easy for administrators to manage and review archived Teams.

The Script

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

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

# Create an array to store archived teams information
$archivedTeams = @()

# Loop through each team and check if it's archived
foreach ($team in $teams) {
    $teamDetails = Get-MgTeam -TeamId $team.Id
    if ($teamDetails.IsArchived -eq $true) {
        # Store archived team details in a custom object
        $archivedTeamInfo = [PSCustomObject]@{
            TeamName = $team.DisplayName
            TeamId   = $team.Id
            IsArchived = $teamDetails.IsArchived
        }
        $archivedTeams += $archivedTeamInfo
    }
}

# Check if any archived teams are found and display the results
if ($archivedTeams.Count -gt 0) {
    $archivedTeams | Format-Table TeamName TeamId IsArchived -AutoSize
} else {
    Write-Output "No archived teams found."
}

# Disconnect from Microsoft Graph
Disconnect-MgGraph

How the Script Works

  • Connect-MgGraph: The script starts by connecting to Microsoft Graph using the necessary scopes (Group.Read.All and TeamSettings.Read.All) to read group and Team settings.
  • Retrieve All Teams: The script fetches all Teams in the organization by filtering groups that are provisioned as Teams.
  • Check Archiving Status: It loops through each Team using the Get-MgTeam cmdlet to retrieve the details of the Team. The key property IsArchived is used to check if the Team has been archived.
  • Store Archived Teams: For each archived Team, the script stores the Team Name, Team ID, and archived status in a custom object, which is then added to an array.
  • Display Results: If archived Teams are found, they are displayed in a tabular format using Format-Table. If no archived Teams are found, the script outputs a message indicating so.
  • Disconnect: The script ends by disconnecting from Microsoft Graph to ensure security.

Further Enhancements

  • Export to CSV: Modify the script to export the archived Teams data to a CSV file for reporting or auditing purposes:
  • $archivedTeams | Export-Csv -Path "C:\Path\To\ArchivedTeams.csv" -NoTypeInformation
  • Additional Team Details: Enhance the script by retrieving and displaying more details about the archived Teams, such as the Team owner, creation date, or number of members.
  • Automated Reporting: Schedule the script to run periodically (e.g., monthly) to generate a report of archived Teams and email it to relevant stakeholders.

Possible Errors & Solutions

Error Cause Solution
Insufficient privileges to complete the operation. The account used to run the script does not have sufficient permissions. Ensure that the account has Group.Read.All and TeamSettings.Read.All permissions. You may need to re-authenticate with elevated privileges or contact your admin for access.
No archived teams found. The script could not find any Teams with the IsArchived status set to true. Verify that the Teams you're checking have been properly archived. If you believe there should be archived Teams, check the archiving status in the Microsoft Teams admin center.
Get-MgTeam : Resource not found for the segment 'Team'. The Team ID provided is invalid or cannot be accessed. Ensure that the Team IDs are valid by using Get-MgGroup to list all Teams and confirm their IDs.
Authorization_RequestDenied The token used for authentication does not have the necessary permissions. Use the Connect-MgGraph cmdlet with the appropriate scopes (Group.Read.All, TeamSettings.Read.All) and reconnect.

Conclusion

This script simplifies the process of retrieving all archived Microsoft Teams, displaying them in a clean, easy-to-read format. By leveraging Graph PowerShell, administrators can automate this task and enhance their reporting capabilities, ensuring that archived Teams are properly managed and preserved.

Suggested Reading

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