Create Microsoft Team with PowerShell and MS Graph API

The next Post in my GRAPH API Series now switches to Microsoft Teams and a heavily used Task. Create a Microsoft Team with PowerShell and Microsoft Graph API.

To learn more from Microsoft GRAPH API, see my Blog Series:
Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams Lis and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – this one – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

API Reference and Permissions

We used the following Docs to get this Script up and running

and I configured the following Permissions

  • User.Read.All
  • User.ReadWrite.All
  • Directory.Read.All
  • Directory.ReadWrite.All
  • Team.Create
  • Teamwork.Migrate.All
  • Group.ReadWrite.All
  • Directory.ReadWrite.All

The Script

$clientID = "your ID"
$Clientsecret = "your Secret"
$tenantID = "Your Tenant"

$TeamName="Techguy Team"
$TeamDescription="The official Team for Techguy.at"
$TeamVisibility="public" #public, private
$Owner="michael@techguy.at"


#Connect to GRAPH API
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}


#Get Owner ID

$URLOwnwer = "https://graph.microsoft.com/v1.0/users/$Owner"
$ResultOwner = Invoke-RestMethod -Headers $headers -Uri $URLOwnwer -Method Get


#Create Teams
$BodyJsonTeam = @"
            {
               "template@odata.bind":"https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
               "displayName":"$TeamName",
               "description":"$TeamDescription",
               "visibility":"$TeamVisibility",
               "members":[
                  {
                     "@odata.type":"#microsoft.graph.aadUserConversationMember",
                     "roles":[
                        "owner"
                     ],
                     "user@odata.bind":"https://graph.microsoft.com/v1.0/users/$($ResultOwner.id)"
                  }
               ]
            }
"@
            $URLTeam = "https://graph.microsoft.com/v1.0/teams"

            Invoke-RestMethod -Headers $headers -Uri $URLTeam -Method POST -Body $BodyJsonTeam 




GitHub Repo

Here you can find the GitHub Repo with a lot of other examples: Seidlm/Microsoft-Graph-API-Examples (github.com)

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published. Required fields are marked *

*

%d bloggers like this: