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.

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

#variables
$webhookurl = "https://outlook.office.com/webhook/561b5378-8100-418a-b31c-89fad5e41b02@9a416268-9869-4193-a4b3-50aaed5e71bc/IncomingWebhook/91378b03b14848c7b19fd99fa42f6f62/31a3d457-ed3f-499a-84e9-40bdf3dec8aa"
$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 https://outlook.office.com/webhook/561b5378-8100-418a-b31c-89fad5e41b02@9a416268-9869-4193-a4b3-50aaed5e71bc/IncomingWebhook/91378b03b14848c7b19fd99fa42f6f62/31a3d457-ed3f-499a-84e9-40bdf3dec8aa

there you go:

Links

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