Monday, 10 December 2018

Archived and Restore teams in Microsoft Teams using PowerShell without user credentials

Problem Statement : As we are aware recently Microsoft has introduced a feature in Teams that Owner of the site can Archive and restore the teams using a few clicks. Though we want to Automate this process using PowerShell with Task scheduler so no manual intervention required.

Solution:  We have developed a PowerShell solution from which we find Inactivity of teams in the last 60 days based on Email ,chat and File Accessed with associated team site. if Powershell script running on Task Scheduler find the teams is inactive for 60 days it Archive the Teams. Archival of the site is not available with Powershell command yet but MS has made available using Graph API.

Step 1: Access Graph API using Powershell

Once you followed the Above Article you should be able to Know the APP ID, App Secret and AADDomain.

Step 2 :Provide Admin consent for this newly created App 

form the URL as below and paste in your broswer.{Yourtenant}/adminconsent?client_id={YourAPPIID}&state=12345&redirect_uri={YourAppRedirectURL}

You need to be part of Administrator, once you given the Admin consent will be able to get Access token using this App for Graph API

Step 3: once you get the Access token its very easy to do the Archival of Team pf Restore.

Please follow the below code.

Connect-PnPOnline -AppId 'YourAppID' -AppSecret 'Your App Secret ID' -AADDomain ''

# Get token to connect the Graph API
 $token = Get-PnPAccessToken

 $headers = @{"Content-Type" = "application/json" ;           "Authorization" = "Bearer " + $token}
For Archive below is the URL formation

$SecScoreURI = ""+YourTeamGroupID+"/archive"

For Restore below is the URL formation  

$SecScoreURI = ""+YourTeamGroupID+"/unarchive"

#Execute the Graph API
$output = Invoke-RestMethod -Uri $SecScoreURI -Headers $headers -Method POST  

And boom your Teams Archive and UnArchive will be done using PowerShell. Getting the Group ID is very easy if you are team Owners.

 Note - you need to be Owner of the teams

let me know if any issue faced