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.

Business vector created by freepik – www.freepik.com


No comments yet

Leave a Reply


Web developer focused on SharePoint branding, blogger, tech enthusiast. Travelling and sports are my addictions, knowledge and success are my daily motivations.