Create a Channel in Teams with PowerShell and MS Graph API

In this post, I want to show you how you can create a Team Channel in Teams with PowerShell using MS GRAPH API.

Some of the Use cases❗
✔️Make sure not everyone can create a new Channel
✔️Control your Channels outside Teams.
✔️Many more…

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
  • TeamMember.ReadWrite.All
  • Channel.Create.Group*
  • Channel.Create
  • Teamwork.Migrate.All
  • Group.ReadWrite.All**
  • Directory.ReadWrite.All**

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

The Script

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


$TeamName = "Techguy Team"
$ChannelName = "My first Channel"
$ChannelDescription = "This is my Channel Description"
$ChannelPrivacy = "Standard" #Standard, Private
$ChannelOwner = "michael@techguy.at" #UPN of the Owner, needed for Private Channel

#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


#Get Team ID
$URLTeam = "https://graph.microsoft.com/v1.0//groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"
$ResultTeam=(Invoke-RestMethod -Headers $headers -Uri $URLTeam -Method Get).value | Where-Object -Property displayName -Value $TeamName -eq



#Create Channel
if ($ChannelPrivacy -eq "Standard") {
    $BodyJsonChannel = @"
            {
               
               "displayName":"$ChannelName",
               "description":"$ChannelDescription",
               "membershipType":"$ChannelPrivacy"
               
            }
"@
}
else {


    $BodyJsonChannel = @"
            {
            "@odata.type": "#Microsoft.Graph.channel",
            "displayName":"$ChannelName",
            "description":"$ChannelDescription",
            "membershipType":"$ChannelPrivacy",
            "members":
                [
                    {
                    "@odata.type":"#microsoft.graph.aadUserConversationMember",
                    "user@odata.bind":"https://graph.microsoft.com/v1.0/users('$ChannelOwner')",
                    "roles":["owner"]
                    }
                ]
            }
"@
}

$URL = "https://graph.microsoft.com/v1.0/teams/$($ResultTeam.id)/channels" 
Invoke-RestMethod -Headers $headers -Uri $URL -Method POST -Body $BodyJsonChannel




GitHub Repo

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

Delegate createa Teams Channel as Self Service with au2mator

With au2mator Self Service Portal, you can create a Service and delegate the task to create a Channel in a Microsoft Teams Team.

Some of the Use cases❗
✔️ Make sure not everyone can create a new Channel
✔️Control your Channels outside Teams.
✔️Many more…

More Details: www.au2mator.com

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published.

*

%d bloggers like this: