Send CARD Message to a Teams Channel with PowerShell and MS GRAPH API

The next Post in my GRAPH API Series now switches to Microsoft Teams. Let us send a CARD Message to a Teams Channel.

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 List and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – Some Examples – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

API Reference and Permissions

Let see the following Docs for more Details.

In this case, we are using Delegated Permissions, so you have to create a User and add the User as a Team Member.

The Script

$clientID = "your ID"
$USer="your User"
$PW="your PW"
$resource = ""

$TeamName="Techguy Team"
$ChannelName="Testing Channel"

#Connect to GRAPH API
$tokenBody = @{  
    Grant_Type = "password"  
    Scope      = ""  
    Client_Id  = $clientId  
    username   =  $User
    password   = $pw
    resource  = $resource

$tokenResponse = Invoke-RestMethod "" -Method Post -ContentType "application/x-www-form-urlencoded" -Body $tokenBody -ErrorAction STOP
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"

#Get ID for the Team
$TeamID=((Invoke-RestMethod -Method GET -Uri $URLgetteamid  -Headers $headers).value | Where-Object -property displayName -value $TeamName -eq).id

#Get ID for the Channel
$ChannelID=((Invoke-RestMethod -Method GET -Uri $URLgetchannelid  -Headers $headers).value | Where-Object -property displayName -value $ChannelName -eq).id

#Send Message in channel

$BodyJsonTeam = @"
    "subject": null,
    "body": {
        "contentType": "html",
        "content": "<attachment id=\"74d20c7f34aa4a7fb74e2b30004247c5\"></attachment>"
    "attachments": [
            "id": "74d20c7f34aa4a7fb74e2b30004247c5",
            "contentType": "application/",
            "contentUrl": null,
            "content": "{\r\n  \"title\": \"This is an example of posting a card\",\r\n  \"subtitle\": \"<h3>This is the subtitle</h3>\",\r\n  \"text\": \"Here is some body text. <br>\\r\\nAnd a <a href=\\\"\\\">hyperlink</a>. <br>\\r\\nAnd below that is some buttons:\",\r\n  \"buttons\": [\r\n    {\r\n      \"type\": \"messageBack\",\r\n      \"title\": \"Login to FakeBot\",\r\n      \"text\": \"login\",\r\n      \"displayText\": \"login\",\r\n      \"value\": \"login\"\r\n    }\r\n  ]\r\n}",
            "name": null,
            "thumbnailUrl": null

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

The Result

In Teams Channel, we see the following Result.

GitHub Repo

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

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

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