Get archived teams in Microsoft Teams using PowerShell

Featured image

In this blog, we shall check how to get archived teams in Microsoft Teams using PowerShell. Install SharePoint PnP Online PowerShell before running this script.

This script uses Get Teams

This PowerShell script prints and exports all the archived teams.

function Export-ArchivedTeams
{   
     param (   
           $ExportPath
           )   
    process{
            Connect-PnPOnline -Scopes "Group.Read.All","Group.ReadWrite.All"
            $accesstoken =Get-PnPAccessToken
            $group = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri  "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/any(c:c+eq+`'Team`')" -Method Get
            $TeamsList = @()
            
            $i=1
            do
            {
                foreach($value in $group.value)
                {
                    Write-Progress -Activity "Get All Groups" -status "Found Group $i" -percentComplete ($i / $group.value.count*100)                
            
                        $id= $value.id
                        Try
                        {
                            $team = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri  https://graph.microsoft.com/beta/teams/$id -Method Get
                            if($team -ne $null -and $team.isArchived -eq $true)
                            {
                                $Teams = "" | Select "TeamsName","TeamType"
                                        
                                $Teams.TeamsName = $value.displayname
                                $Teams.TeamType = $value.visibility
                                     
                                $TeamsList+= $Teams
                                $Teams=$null
                            }
                        }
                        Catch
                        {
                            $team = $null
                        }
                        $i++
                }
        
                if ($group.'@odata.nextLink' -eq $null )
                {
                    break
                }
                else
                {
                    $group = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri $group.'@odata.nextLink' -Method Get
                }
            }while($true);
            $TeamsList
            $TeamsList |Export-csv $ExportPath -NoTypeInformation
        }
}
Export-ArchivedTeams -ExportPath "C:\temp\Archivedteams.csv"