How to create a team with PnP PowerShell
Sometimes I like a nice challenge. I came across a Tweet from Joanne Klein with a question about Microsoft Teams provisioning. This article describes how to create a team with PnP PowerShell and the Microsoft Graph.
The question from Joanne
On the 23rd of january I spotted this Tweet and the first response from Drew Madelung pointed me in the right direction for a solution.
You could call graph to create via PS and use PnP to handle graph connection or use Teams module
— Drew Madelung (@dmadelung) January 24, 2019
My first attempt to create a solution
The following script is the end result of my attempt to recreate the solution suggested by Drew Madelung. I choose to use Microsoft Graph and PnP PowerShell.
Feel free to use it (of course at your own risk ;)).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# Connect with the Microsoft Graph via PnP PowerShell # https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps Connect-PnPOnline -Scopes "Group.ReadWrite.All" $accesstoken = Get-PnPAccessToken # Example of json for POST https://graph.microsoft.com/v1.0/groups # https://docs.microsoft.com/en-us/graph/api/group-post-groups?view=graph-rest-1.0 $creategroup = @' { "description": "Twitter Test Team", "displayName": "Twitter", "groupTypes": [ "Unified" ], "mailEnabled": true, "mailNickname": "twitter", "securityEnabled": false } '@ $response = Invoke-RestMethod -Uri https://graph.microsoft.com/v1.0/groups -Body $creategroup -ContentType "application/json" -Headers @{Authorization = "Bearer $accesstoken"} -Method Post # Example of json for PUT https://graph.microsoft.com/v1.0/groups/{id}/team # https://docs.microsoft.com/en-us/graph/api/team-put-teams?view=graph-rest-1.0 $groupid = $response.'id' $createteam = @' { "memberSettings": { "allowCreateUpdateChannels": true }, "messagingSettings": { "allowUserEditMessages": true, "allowUserDeleteMessages": true }, "funSettings": { "allowGiphy": true, "giphyContentRating": "strict" } } '@ $createteamuri = "https://graph.microsoft.com/v1.0/groups/" + $groupid + "/team" Invoke-RestMethod -Uri $createteamuri -Body $createteam -ContentType "application/json" -Headers @{Authorization = "Bearer $accesstoken"} -Method PUT |
Breakdown of the solution
Basicly the script has three parts. First of all you need to connect with the Microsoft Graph and get an access token. I am using the latest PnP PowerShell module for this, so make sure you download this module before running the script.
After that you can use a POST request to create an Office 365 Group via the Microsoft Graph. This is the first step in the creation of a Team.
In the second step I am using the Group ID of the reponse to use a PUT request to create a Microsoft Team.