Create Teams using PowerShell
Creating multiple Teams on Microsoft Teams can be a tedious process, but you can easily automate it with the use of Microsoft Graph and PowerShell.
In this article I’m providing two scripts to achieve the same result; the first one uses Teams PowerShell cmdlets while the second one uses PnP PowerShell and Microsoft Graph.
On both examples I’m creating the Team and adding two channels to it.
Using Teams PowerShell
Teams PowerShell can be found here, with instructions and all the available commands.
Connect-MicrosoftTeams $group = New-Team -DisplayName "HANDS ON Teams" -Description "Team to post technical articles and blogs" -AccessType Public Add-TeamUser -GroupId $group.GroupId -User "joao@handsonsp.onmicrosoft.com" -Role "owner" New-TeamChannel -GroupId $group.GroupId -DisplayName "Article Ideas" New-TeamChannel -GroupId $group.GroupId -DisplayName "Social Media"
Using PnP PowerShell and Microsoft Graph
PnP PowerShell can be found here, and instructions about Microsoft graph can be found here.
PnP PowerShell is used to handle the connection to Graph, all the actions are then made using Microsoft Graph.
# Connect to AAD to consume the Microsoft Graph $arrayOfScopes = @("Group.ReadWrite.All", "AppCatalog.ReadWrite.All") Connect-PnPOnline -Scopes $arrayOfScopes $accessToken = Get-PnPAccessToken #Prepare generic OAuth Bearer token header $headers = @{ "Content-Type" = "application/json" Authorization = "Bearer $accessToken" } #Create Office 365 Group $createGroupRequest = @{ displayName = "HANDS ON SharePoint (Graph)" description = "Team Provisioned using Graph" groupTypes = @("Unified") mailEnabled = $true mailNickName = "HOT" securityEnabled = $false visibility = "Private" } $createGroupBody = ConvertTo-Json -InputObject $createGroupRequest $response = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups" -body $createGroupBody -Method Post -Headers $headers -UseBasicParsing $groupId = $response.id # Create the team $createTeamRequest = @{ memberSettings = @{ allowCreateUpdateChannels = $true } messagingSettings = @{ allowUserEditMessages = $true allowUserDeleteMessages = $true } funSettings = @{ allowGiphy = $true giphyContentRating = "strict" } } $createTeamBody = ConvertTo-Json -InputObject $createTeamRequest $response = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups/$groupId/team" -Body $createTeamBody -Method Put -Headers $headers -UseBasicParsing $teamId = $response.id # Create channel #1 $createChannelRequest = @{ displayName = "Article Ideas" description = "Channel to post technical articles and blogs" } $createChannelBody = ConvertTo-Json -InputObject $createChannelRequest Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels" -Body $createChannelBody -Method Post -Headers $headers -UseBasicParsing # Create channel #2 $createChannelRequest = @{ displayName = "Social Media" description = "Channel to promote the blog" } $createChannelBody = ConvertTo-Json -InputObject $createChannelRequest Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels" -Body $createChannelBody -Method Post -Headers $headers -UseBasicParsing
Conclusion
The end result was the same with both solutions as shown in the image below.
If you want to automate simple things like the creation of teams, Teams PowerShell is a good choice and you will be able to do it with a few lines of code and don’t need to worry about parsing responses from the execution of commands.
On the other hand, if you want to build a complex solution you need to use Microsoft Graph as it allows you to setup tabs and apps inside the channels and also provides integrations with all Office 365 Services.
March 18, 2021
Hi. The group creation works for me, but I get an 404 error on the team creation.
what im doing wrong here??
Clear-Host
$env:graphApiDemoAppId = “myappid” # Replace with your Azure AD app id
$env:graphApiDemoAppSecret = “mysecret” # Replace with your Azure AD app secret
$env:tenantId = “mytenantId” # Replace with your Azure AD tenant ID
$oauthUri = “https://login.microsoftonline.com/$env:tenantId/oauth2/v2.0/token”
# Create token request body
$tokenBody = @{
client_id = $env:graphApiDemoAppId
client_secret = $env:graphApiDemoAppSecret
scope = “https://graph.microsoft.com/.default”
grant_type = “client_credentials”
}
# Retrieve access token
$tokenRequest = Invoke-RestMethod -Uri $oauthUri -Method POST -ContentType “application/x-www-form-urlencoded” -Body $tokenBody -UseBasicParsing
# Save access token
$accessToken = ($tokenRequest).access_token
$headers = @{
“Authorization” = “Bearer $accessToken”
“Content-type” = “application/json”
}
try
{
# Create group request body
$groupName = “Test_12”
$groupBodyParams = @{
displayName = $groupName
description = $groupName
mailNickName = $groupName
groupTypes = @(“Unified”)
mailEnabled = $true
securityEnabled = $false
visibility = “Private”
}
$groupBody = ConvertTo-Json -InputObject $groupBodyParams
$newGroup = Invoke-RestMethod -Uri “https://graph.microsoft.com/v1.0/groups” -Method POST -Headers $headers -Body $groupBody
$groupId = $newGroup.id
Write-Host “group created:” $groupName “id:” $groupId
try
{
#convert group to team..
$teamBodyParams = @{
memberSettings = @{
allowCreateUpdateChannels = $true
}
messagingSettings = @{
allowUserEditMessages = $true
allowUserDeleteMessages = $true
}
funSettings = @{
allowGiphy = $true
giphyContentRating = “strict”
}
}
$teamUri = “https://graph.microsoft.com/v1.0/groups/” + $groupId + “/team”
#Write-Host $teamUri
$teamBody = ConvertTo-Json -InputObject $teamBodyParams
$newTeam = Invoke-RestMethod -Uri $teamUri -Method POST -Headers $headers -Body $teamBody
$teamId = $newTeam.id
}
catch
{
Write-Host “createTeam ExceptionMessage:” $_.Exception.Message
Write-Host “createTeam StatusCode:” $_.Exception.Response.StatusCode.value__
Write-Host “createTeam StatusDescription:” $_.Exception.Response.StatusDescription
}
}
catch
{
Write-Host “createGroup ExceptionMessage:” $_.Exception.Message
Write-Host “createGroup StatusCode:” $_.Exception.Response.StatusCode.value__
Write-Host “createGroup StatusDescription:” $_.Exception.Response.StatusDescription
}
May 18, 2021
Hi Iverson,
My guess is that the group is not yet created when the cmdlet for the team creation gets executed.
Have you tried to execute it sections to see if the result is the same.
Have a nice day 🙂