Microsoft Teams – Quickly add a Webhook to a Teams channel and send a message using PowerShell

I just discovered the possibility to add Webhooks to a Teams Channel. Since webhooks are nothing new to me, i wanted to know if my PowerShell Scripts i created when i was playing last time with Webhooks if they’re working with the Teams API as well – surprise, i created a Webhook and sent a Message using PowerShell in under 10 minutes -> it’s THAT SIMPLE!

So where to start ..

Create a Channel in Teams

Create a standard channel

To create a standard channel, start in the team list. Find the team name and click More options More options button > Add channel. You can also click Manage team, and add a channel in the Channels tab. 

Create a Teams Channel

Give it a name, optionally a description and choose Standard under Privacy -> you can change that later, but to make it run the first time -> optimistic approach, once it’s working, you can close it down and make it more secure…

Also you can decide whether you want to list the channel in the ‘everyone’s channel list’ .. well it’s up to you.

Next is – adding a Connector to the channel

Add Connector to a Teams Channel

Search for Webhook

Now click on Add or Configure the Incoming Webhook

Configure Incoming Webhook

Give it a name and click on Create (on the screenshot the button already changed to Done..) Now, copy the created URL and paste in a notebook..

must look similar to this

Now fire-up Powershell_ise or Visual Studio Code… and here’s the script. Just replace the Webhook URL with the one just created before and run the script.

	Send message using PowerShell to a Teams Channel using Incoming Webhook
	The purpose of the script is to demonstrate the use pf PowerShell, Invoke-Restmethod and Teams Channels
	Author:  Christian Casutt
	Version: 1.0
	Date: 2021-01-07

$webhookurl = ""
$message = $msg = "$($env:USERNAME) on $($env:COMPUTERNAME) sent this message at $(Get-Date)"

Invoke-Restmethod -Headers @{'accept'='application/json'} -Body (@{'text'= '' + $message + ''} | ConvertTo-Json) -Method POST -URI $webhookurl

# or as a one-liner 
Invoke-Restmethod -Headers @{'accept'='application/json'} -Body (@{'text'= ''+"$($env:USERNAME) on $($env:COMPUTERNAME) sent this message at $(Get-Date)"+''} | ConvertTo-Json) -Method POST -URI

there you go:


Create a channel in Teams
Post external requests to Teams with incoming webhooks