Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Send Telegram Messages from a PowerShell Script

March 12, 2024

Send Telegram Messages from a PowerShell Script

You can use your Telegram messenger as a notification tool to get instant reports on various infrastructure events, script execution results, or scheduler tasks. This article shows you how to use PowerShell to send a text notification to a Telegram channel or group through the Bot API.

First, create a new Telegram bot using @BotFather. Find it in your Telegram client and send it the following commands:
/start
/newbot

Specify the bot’s name and username. BotFather will generate an HTTP token that you must copy and save.

Create New Telegram bot with BotFather

To send a message to a Telegram chat or a specific user, you need to know their ID. In this example, I will send notifications to myself, so use @my_id_bot to find my Telegram ID:

/start

Your user ID: 987654321

To send a message to any Telegram chat, you need to specify the bot token and the ID of the target user (or chat):

$tg_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$tg_chat_id="987654321"

The TLS 1.2 protocol is required to connect to the Telegram API. Ensure that the TLS 1.2 protocol version is enabled in  Windows. By default, PowerShell uses the legacy SSL 3.0, TLS 1.0, or TLS 1.1 protocols for connections. Run the following command to use TLS version 1.2 in the current PowerShell session

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

And here’s a command to send a message to Telegram:

$message="Test message alert from PowerShell"
$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($tg_token)/sendMessage?chat_id=$($tg_chat_id)&text=$($Message)"

Once you run that one, you should get a message from the bot.

You can use emoji and HTML text formatting to make notifications more visually appealing and readable:

$message= $currend_data + "⚠️ Update Script <b>SAP_DB_Update</b> completed with errors"
$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($tg_token)/sendMessage?chat_id=$($tg_chat_id)&text=$($Message)&parse_mode=html"

PowerShell: sending messages to Telegram via bot

To work with PowerShell scripts, I recommend using the VS Code editor.

PowerShell script to send message via Telegram API

If you are accessing the Internet through a proxy server, you can use the -Proxy parameter of the Invoke-WebRequest cmdlet to specify the proxy settings. Use the -ProxyCredential argument to authenticate to a proxy.

$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($Telegramtoken)/sendMessage?chat_id=$($Telegramchatid)&text=$($Message)" –Proxy "http://192.168.13.155:3128"

In version PowerShell Core 7.x+, the Invoke-WebRequest cmdlet uses the proxy settings specified in the environment variables. Learn more about using a proxy in PowerShell.

[/alert]

You can create a function from a script that sends a message to Telegram and add it to the PowerShell profile file in Windows:

function Send-Telegram {
        [CmdletBinding()]
        param(
            [Parameter()]
            [string] $Message
        )    
        $tg_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        $tg_chat_id="987654321"
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        $Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($tg_token)/sendMessage?chat_id=$($tg_chat_id)&text=$($Message)&parse_mode=html" 
        return $Response    
 }
Open a text file with a PowerShell profile that is automatically applied when powershell.exe/pwsh.exe starts:

notepad $PSHOME\Profile.ps1

PowerShell function to send a Telegram message

It is now possible to send a message to a Telegram channel from any PowerShell script.

Send-Telegram "My test message"

If you use Teams as your primary messenger, you can also use PowerShell to send a message to a Teams channel.
0 comment
1
Facebook Twitter Google + Pinterest
PowerShellWindows 10Windows Server 2019
previous post
Managing VMware Infrastructure with Ansible
next post
Deploying Microsoft Office Language Packs

Related Reading

Wi-Fi (Internet) Disconnects After Sleep or Hibernation on...

March 15, 2024

Fix: Remote Desktop Licensing Mode is not Configured

August 24, 2023

How to Install Remote Server Administration Tools (RSAT)...

March 17, 2024

How to Delete Old User Profiles in Windows

March 15, 2024

Managing Windows Firewall Rules with PowerShell

March 11, 2024

How to Force Remove a Printer That Won’t...

March 15, 2024

How to Allow Non-Admin User to Start/Stop Service...

March 15, 2024

Updating PowerShell Version on Windows

March 12, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Delete Old User Profiles in Windows
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Configuring Port Forwarding in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

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


Back To Top