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
- Install-Module SharePointPnPPowerShellOnline
Register the PowerShell Application in Azure
On a Click of “+ New Registration” link as highlighted above, you will be redirected to the below screen:
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.
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.
Select Microsoft Graph API as shown below:
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
Click Grant admin consent as highlighted below for the app:
Once the Admin consent is granted successfully, the screen is displayed as shown below:
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.
Click on “Certificates & secrets” as highlighted above to generate a secret key which will be used in PowerShell.
Click New client secret as highlighted above.
Enter the Description and click on the “Add” button to generate Client Secret. You will have the below screen with a Secret value generated.
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:
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
- #Connect App
- Connect-PnPOnline -AppId $ApplicationID -AppSecret $ClientSecret -AADDomain $ActiveDirectorydomainName
- # Get Access token of the App
- $token = Get-PnPAccessToken
Get All MS Teams in the tenant
- $GetAllTeamsAPIURL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"
- $headers = @{"Authorization"="Bearer " + $token;"Content-Type"= "application/json";}
- $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
- foreach($value in $output.value) {
- if ($value.displayName - eq $TeamName) {
- $TeamID = $value.id
- $ArchiveTeamAPIURL = "https://graph.microsoft.com/v1.0/teams/" + $TeamID + "/archive"
- $response = Invoke - RestMethod - Uri $ArchiveTeamAPIURL - Headers $headers - Method "POST"
- }
- }
Complete PowerShell Script
- #Powershell script to Archive an MS team
- #Created By: Vinit Kumar
- # Variable - Change the parameter as it need
- $ApplicationID = "9381bf0d-6128-478e-b1fd-5a0ed48d37d5"
- # Application ID as we have created during the Register application process
- $ClientSecret = "jvo0J3hmDk.Be_1ZE31dkCt0GPDhM.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
- #Ends
- #Connect App
- Connect - PnPOnline - AppId $ApplicationID - AppSecret $ClientSecret - AADDomain $ActiveDirectorydomainName
- # Get Access token of the App
- $token = Get - PnPAccessToken
- # Get all Teams availabe in Tenant
- $GetAllTeamsAPIURL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"
- $headers = @ {
- "Authorization" = "Bearer " + $token;
- "Content-Type" = "application/json";
- }
- $output = Invoke - RestMethod - Uri $GetAllTeamsAPIURL - Headers $headers - Method GET
- #Ends
- #Loop all the Teams, Match with Team name we want to archive and call Archive Team Graph API
- foreach($value in $output.value) {
- if ($value.displayName - eq $TeamName) {
- $TeamID = $value.id
- $ArchiveTeamAPIURL = "https://graph.microsoft.com/v1.0/teams/" + $TeamID + "/archive"
- $response = Invoke - RestMethod - Uri $ArchiveTeamAPIURL - Headers $headers - Method "POST"
- }
- }
- #Ends
Output of Archived MS team
After execution of the script, your teams will be archived as shown in the below image
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.