How To Archive MS Teams Using Graph API In Powershell

Introduction

 
This article demonstrates how to Archive MS Teams using Microsoft Graph API in PowerShell. As an Administrator of MS teams for an organization, you want to archive or delete a team at the end of the project. There are scenarios where MS teams will remain unused after projects are finished and it requires archival using automation. The best way to achieve the team's archival automation process using PowerShell is with the help of Graph API.
 
When you archive a team, all activity for that team is stopped. You can still add or remove members and you can still view all the team activity in channels, files, and chats.
In this article, we will use Microsoft Graph API to archive the teams within PowerShell script. First, we need to register the PowerShell Module in Azure Active Directory, Provide Admin Consent to the app, and finally Archive the Teams using Graph API
 
Prerequisites
  • An Azure Active Directory
  • You should have the Global Administrator role to provide consent to the app
  • Windows PowerShell 3.0
  • SharePointPnpPowerShellOnline Module
Please install the SharePointPnpPowerShellOnline module if it’s not already present using the below command.
  1. Install-Module SharePointPnPPowerShellOnline  

Register the PowerShell Application in Azure

 
Go to the Azure Active Directory App registration page as shown below:
 
How To Archive MS Teams Using Graph API In Powershell
 
On a Click of “+ New Registration” link as highlighted above, you will be redirected to the below screen:
 
How To Archive MS Teams Using Graph API In Powershell
 
Enter the Name of Application and click on the “Register” button as highlighted above. The user will be redirected to an overview screen of the app as below after registration of the app is successful.
 
How To Archive MS Teams Using Graph API In Powershell
 
Click on “API permissions” to provide app permission to access the Graph API, the user will be redirected to the below screen. Click on “Add a permission” as Highlighted above.
 
How To Archive MS Teams Using Graph API In Powershell
 
Select Microsoft Graph API as shown below:
 
How To Archive MS Teams Using Graph API In Powershell

Click on “Application Permissions”.
 
Select the below permissions and click on Add Permission as highlighted below:
  • Directory.ReadWrite.All
  • Group.ReadWrite.All
  • Team.ReadBasic.All
  • TeamSettings.ReadWrite.All
How To Archive MS Teams Using Graph API In Powershell
 
Click Grant admin consent as highlighted below for the app:
 
How To Archive MS Teams Using Graph API In Powershell

Once the Admin consent is granted successfully, the screen is displayed as shown below:
 
How To Archive MS Teams Using Graph API In Powershell
 
Go to the Overview of the App. Copy the Application (client) ID value as highlighted in the below screen. This value will be used in PowerShell for authentication. 
 
How To Archive MS Teams Using Graph API In Powershell
  
Click on “Certificates & secrets” as highlighted above to generate a secret key which will be used in PowerShell.
 
How To Archive MS Teams Using Graph API In Powershell
 
Click New client secret as highlighted above.
 
How To Archive MS Teams Using Graph API In Powershell


Enter the Description and click on the “Add” button to generate Client Secret. You will have the below screen with a Secret value generated.
 
How To Archive MS Teams Using Graph API In Powershell
 
Copy the secret value which will be used in PowerShell for authentication.
 
So using the above steps we have got Application ID and Client Secret which will be used in PowerShell Script.
 

PowerShell Script to Archive the Team

 
MS teams to archive:
 
How To Archive MS Teams Using Graph API In Powershell
 
Variables Explanations in this Articles
  • $ApplicationID="9381bf0d-6128-478e-b1fd-5a0ed48d37d5" # Application ID as we have created during the Register application process
  • $ClientSecret="jvo0J3hmDk.Be_1ZE30dkCt0GPDhM.1S~." # Application ID as we have created during the Register application process
  • $ActiveDirectorydomainName="263994CTS.onmicrosoft.com" # Active Directory Domain name
  • $TeamName="Test Team to Archive"# Display Name of the Team you want to archive. You can use other condition as well to archive the team Like there is no conversation or SPO activity for last 90 days
Connect the Graph API and get the Access Token
  1. #Connect App    
  2. Connect-PnPOnline -AppId $ApplicationID -AppSecret $ClientSecret -AADDomain $ActiveDirectorydomainName    
  3. # Get Access token of the App    
  4. $token = Get-PnPAccessToken     
Get All MS Teams in the tenant
  1. $GetAllTeamsAPIURL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"    
  2. $headers = @{"Authorization"="Bearer " + $token;"Content-Type""application/json";}    
  3. $output = Invoke-RestMethod -Uri $GetAllTeamsAPIURL -Headers $headers -Method GET     
Call the Invoke Rest Method to get the All MS teams by Passing the Token and store in $Output variable. 
 
Loop and Archive the Team
 
Loop all the teams, match with the team name we want to archive, and call Archive Team Graph API 
  1. foreach($value in $output.value) {  
  2.     if ($value.displayName - eq $TeamName) {  
  3.         $TeamID = $value.id  
  4.         $ArchiveTeamAPIURL = "https://graph.microsoft.com/v1.0/teams/" + $TeamID + "/archive"  
  5.         $response = Invoke - RestMethod - Uri $ArchiveTeamAPIURL - Headers $headers - Method "POST"  
  6.     }  
  7. }   
Complete PowerShell Script
  1. #Powershell script to Archive an MS team  
  2. #Created By: Vinit Kumar  
  3. # Variable - Change the parameter as it need  
  4. $ApplicationID = "9381bf0d-6128-478e-b1fd-5a0ed48d37d5"  
  5. # Application ID as we have created during the Register application process  
  6. $ClientSecret = "jvo0J3hmDk.Be_1ZE31dkCt0GPDhM.1S~-"  
  7. # Application ID as we have created during the Register application process  
  8. $ActiveDirectorydomainName = "263994CTS.onmicrosoft.com"  
  9. # Active Directory Domain name  
  10. $TeamName = "Test Team to Archive"  
  11. # Display Name of the Team you want to archive.You can use other condition as well to archive the team Like there is no conversation or SPO activity  
  12. for last 90 days  
  13. #Ends  
  14. #Connect App  
  15. Connect - PnPOnline - AppId $ApplicationID - AppSecret $ClientSecret - AADDomain $ActiveDirectorydomainName  
  16. # Get Access token of the App  
  17. $token = Get - PnPAccessToken  
  18. # Get all Teams availabe in Tenant  
  19. $GetAllTeamsAPIURL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"  
  20. $headers = @ {  
  21.     "Authorization" = "Bearer " + $token;  
  22.     "Content-Type" = "application/json";  
  23. }  
  24. $output = Invoke - RestMethod - Uri $GetAllTeamsAPIURL - Headers $headers - Method GET  
  25. #Ends  
  26. #Loop all the Teams, Match with Team name we want to archive and call Archive Team Graph API  
  27. foreach($value in $output.value) {  
  28.     if ($value.displayName - eq $TeamName) {  
  29.         $TeamID = $value.id  
  30.         $ArchiveTeamAPIURL = "https://graph.microsoft.com/v1.0/teams/" + $TeamID + "/archive"  
  31.         $response = Invoke - RestMethod - Uri $ArchiveTeamAPIURL - Headers $headers - Method "POST"  
  32.     }  
  33. }  
  34. #Ends  
Output of Archived MS team
 
After execution of the script, your teams will be archived as shown in the below image
 
How To Archive MS Teams Using Graph API In Powershell
 

Summary

 
In this article, I discussed how we can Archive the MS team using Graph API in PowerShell. We have discussed how to register the APP in Azure Active directory, provide permission to the app to connect Graph API. Automation of the archival of the Teams is necessary when IT maintains and governs the Teams within an organization. A Global Admin can get the activity details of a Team in PowerShell to find out if it needs to be archived.


Similar Articles