How To Clone a Team Using Microsoft Graph API In PowerShell

In this blog, you will see how to clone a team using Microsoft Teams Graph API in PowerShell.
 
Graph API can be used to automate Microsoft Teams lifecycle such as creating teams, channels, adding members etc.
 
Refer to this link to see the list of Graph API’s available for Microsoft Teams.
 
Prerequisites
 
Register an application in Azure and add Group.ReadWrite.All permissions. Refer to my previous article on “How to Access Microsoft Teams Graph API in Power Automate” to register an application in Azure Portal. Get the client ID and secret which will be used in script for authentication.
 

Clone Team Script

 
Step 1
 
Open Notepad and paste the following code. Save the file as CloneTeam.ps1.
 
Step 2
 
Open PowerShell window. Navigate to the folder path where the script file is saved.
  1. # Input Parameters  
  2. $clientId = "c714f180-c8cc-4f70-b720-409c798274a9"  
  3. $clientSecret = "fXzmWCUDmeXXqQ@D3b4xxd6GzrAY5[@="  
  4. $tenantName = "c986.onmicrosoft.com"  
  5. $resource = "https://graph.microsoft.com/"  
  6. $URL = "https://graph.microsoft.com/v1.0/teams/daba3aa7-6622-4f75-8e27-a9587c6642dc/clone"  
  7.   
  8. $tokenBody = @{  
  9.     Grant_Type    = "client_credentials"  
  10.     Scope         = "https://graph.microsoft.com/.default"  
  11.     Client_Id     = $clientId  
  12.     Client_Secret = $clientSecret  
  13. }   
  14.   
  15. $body = @’  
  16. {    
  17.      "displayName""Cloned Team",  
  18.      "description""Cloned Team using Graph API",  
  19.      "mailNickname""clonedTeam",  
  20.      "partsToClone""apps,tabs,settings,channels,members",  
  21.      "visibility""public"  
  22. }  
  23. ‘@  
  24.   
  25. $tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $tokenBody  
  26. Invoke-RestMethod -Headers @{Authorization = "Bearer $($tokenResponse.access_token)"} -Uri $URL -Method POST -Body $body -ContentType 'application/json'  
Step 3
 
Execute the following script.
  1. .\CloneTeam.ps1  
Step 4
 
Team cloned successfully.
 
Reference
 
https://docs.microsoft.com/en-us/graph/api/team-clone?view=graph-rest-1.0
 

Summary

 
Thus, in this blog, you saw how to clone  a team using Microsoft Teams Graph API in PowerShell.