Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Microsoft 365 / Teams / How to Send a Message to Teams Channel with PowerShell?

October 4, 2022 PowerShellTeams

How to Send a Message to Teams Channel with PowerShell?

You can send messages to Teams channels from PowerShell using webhook or Microsoft Graph API calls. Let’s see how to send and read messages in Microsoft Teams channels using PowerShell. You can use these PS scripts in various monitoring or notification scenarios where you need to send a notification not via email (Send-MailMessage cmdlet), but directly to a Teams channel.

Contents:
  • Send Messages to Microsoft Teams Using WebHook
  • How to Send or Read a Teams Message with Microsoft Graph API?

Send Messages to Microsoft Teams Using WebHook

You can send messages to a Microsoft Teams channel using the built-in WebHook connectors. A webhook connector is an URL address you can send a JSON object using an HTTP POST request.

  1. Create a channel in Teams. You can do it using the Microsoft Teams PowerShell module. For example: Get-team -DisplayName sysops| New-TeamChannel -DisplayName "AdminAlerts" -MembershipType Private
  2. Then open the Teams client (a desktop or web version) and select Connectors in the context menu of the channel; add connector in microsoft teams client
  3. Add the Incoming Webhook type of connector;teams - adding webhook connector
  4. Specify the connector name;
  5. Copy the URL of the connector that Azure created for you.

copy webhook connector url

To send a simple message to the channel using this URL, run the following PowerShell commands:

$myTeamsWebHook = “https://woshub.webhook.office.com/webhookb2/123456-12312-@aaaaa-bbbb-cccc/IncomingWebhook/xxxxxxxxxxxxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx”
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body '{"text":"Test Teams!"}' -Uri $myTeamsWebHook

Make sure that your message appeared in the Teams channel. The connector you have created is displayed as the author of the message.

send message to teams channel with powershell and webhook

MS Teams allows you to send no more than 4 messages per second.

You can add other data to your Teams notification, change font, color, and add extra information from your PowerShell script.

For example, the following script monitors the user lockout events (Event 4740) on a domain controller with the PDC FSMO role and sends a notification to the Teams channel.

$LockedUser = Search-ADAccount -UsersOnly –LockedOut | Get-ADUser –Properties lockoutTime, emailaddress | Select-Object emailaddress, @{n='lockoutTime';e={[DateTime]::FromFileTime($_.lockoutTime)}} | Sort-Object LockoutTime -Descending | Select-Object -first 1
$myTeamsWebHook  = "YOUR-WEBHOOK-URL"
$webhookMessage = [PSCustomObject][Ordered]@{
"@type"      = "FF0000"
"@context"   = "http://schema.org/extensions"
"summary"    = "Locked User: $($LockedUser.SamAccountName) "
"themeColor" = '700015'
"title"      = "User Lockout Event"
"text" = "`n
SamAccountName: $($LockedUser.SamAccountName)
Mail: $($LockedUser.EmailAddress)
Timestamp: $($LockedUser.LockoutTime.ToString()) "
}
$webhookJSON = convertto-json $webhookMessage -Depth 50
$webhookCall = @{
"URI"         = $myTeamsWebHook
"Method"      = 'POST'
"Body"        = $webhookJSON
"ContentType" = 'application/json'
}
Invoke-RestMethod @webhookCall

Then the event appears in the Teams channel and an administrator can react to it. send rich text message to teams with powershell

You may bind the script to EventID 4740 in Event Viewer (learn how to attach a task to an event). Or you can create a job in the Task Scheduler and run the PowerShell script regularly.

How to Send or Read a Teams Message with Microsoft Graph API?

Using Microsoft Graph API, you can both send and read messages in the Teams channel. First, you have to register the Azure app, set permissions (Group.Read.All, ChannelMessage.Send, Chat.ReadWrite, and ChatMessage.Send), and get an authentication token (learn more in the article How to Connect Azure Microsoft Graph API using PowerShell).

$ApplicationID = "4434ad23-b212-3212-3aad-54321de3bbc"
$TenatDomainName = "26216542-aaaa-bbbb-2212-65566aa6c32"
$AccessSecret="12-32Bh654321d3-seLa23l33p.hhj33MM21aaf"
$Body = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $ApplicationID
Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token -Method POST -Body $Body
$token = $ConnectGraph.access_token
$URLchatmessage="https://graph.microsoft.com/v1.0/teams/$TeamID/channels/$ChannelID/messages"
$BodyJsonTeam = @"
{
"body": {
"content": "Hello World"
}
}
"@
Invoke-RestMethod -Method POST -Uri $URLchatmessage -Body $BodyJsonTeam -Headers -Headers @{Authorization = "Bearer $($token)"}

You can get $TeamID and $ChannelID using Get-Team and Get-TeamChannel from the MicrosoftTeams module.

In the same way, you can read messages from a Teams chat using the GET method.

1 comment
0
Facebook Twitter Google + Pinterest
previous post
Uninstalling Programs with PowerShell in Windows 10/11
next post
Fix: Signature Button Not Working in Outlook 2019/2016/365

Related Reading

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Delete Old User Profiles in Windows?

January 25, 2023

1 comment

Janek October 10, 2022 - 1:09 pm

Gr8 post! im work on this sin a week, i can add field “add a comment” , “set du date” from powershell – could u teach us how to respodn to webhook?

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Configuring Port Forwarding in Windows
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top