Skip to content

PowerShell ​

The official MQTTBroker module is a pure PowerShell MQTT 3.1.1 client: no DLLs, no Mosquitto, no NuGet packages. It runs on Windows PowerShell 5.1 and PowerShell 7+ on Windows, Linux and macOS, so it suits scheduled tasks, admin scripts and quick tests.

How it connects ​

PowerShell script connecting to ConnectMQ A PowerShell script uses MQTTBroker module · pure PowerShell, no DLLs to connect to broker.connectmq.com over MQTT 3.1.1 · TLS 8883 with a username, password and a client ID based on the issued client ID. The broker checks the client with the Broker API (credentials, client ID, ACL and plan limits) and delivers messages to the web console and other devices and services. Your codeEveryone else on the brokerConnectMQ CloudMQTT 3.1.1 · TLS 8883username · password · client ID <ISSUED_ID>_<suffix>authPowerShell scriptMQTTBroker module · pure PowerShell, no DLLs$c = Connect-MQTTBroker -Tls … then Publish-MQTTMessageWindows PowerShell 5.1 and PowerShell 7+ · scheduled tasks, admin scriptsMQTT brokerbroker.connectmq.comTLS 8883 · WSS 8084 /mqtt · MQTT 3.1.1 and 5Broker APICredentials · client IDACL · plan limitsWeb consoleapp.connectmq.com · live topics · alertsDevices & servicesESP32 · ConnectMQ Edge · back ends · appsMQTT messagesConnection check (credentials, client ID, ACL)

Install ​

powershell
Install-Module MQTTBroker -Scope CurrentUser
CommandWhat it does
Connect-MQTTBrokerOpens a connection and returns it for reuse
Publish-MQTTMessagePublishes a string or byte[] payload; accepts pipeline input
Receive-MQTTMessageSubscribes and streams messages to the pipeline
Disconnect-MQTTBrokerSends DISCONNECT and closes the socket
Test-MQTTBrokerConnects, publishes, receives and reports timings
New-MQTTTopicPrefix, Get-MQTTBrokerInfoHelpers for the public test broker

Use Get-Help <command> -Full for every parameter.

Connect to your broker ​

Open a connection with your credentials, then pass it to the other commands with -Connection:

powershell
$c = Connect-MQTTBroker -Tls `
    -ClientId "$($env:CMQ_CLIENT_ID)_ps-$env:COMPUTERNAME" `
    -Username $env:CMQ_USERNAME `
    -Password (Read-Host -AsSecureString 'Password')

Publish-MQTTMessage -Connection $c -Topic 'acme/istanbul/assembly/line1/pc-01/status' -Message '{"online":true}' -Qos 1

Disconnect-MQTTBroker $c

-Tls connects to broker.connectmq.com on port 8883 with TLS 1.2 and checks the certificate against the system CA store. -Password takes a SecureString or a plain string.

Credentials only go through Connect-MQTTBroker

Publish-MQTTMessage and Receive-MQTTMessage have no -ClientId, -Username or -Password. Without -Connection they connect anonymously with a random ps-xxxxxxxx client ID, which only works on the public test broker. For your private broker, always call Connect-MQTTBroker with -ClientId set to your issued client ID plus a suffix (why), and pass the result as -Connection.

Connect-MQTTBroker parameterDefaultNotes
-TlsoffUse it: port 8883 with TLS. Without it, plain TCP on port 1883
-BrokerHostbroker.connectmq.com
-Port1883 / 8883
-ClientIdps- + randomSet it to <issued client ID>_<suffix>
-Username, -PasswordnoneFrom MQTT Connection Settings
-KeepAlive60Seconds
-TimeoutSeconds10Connect timeout

Publish ​

powershell
# Send CPU load every 5 seconds
while ($true) {
    $cpu = (Get-CimInstance Win32_Processor | Measure-Object LoadPercentage -Average).Average
    $json = @{ cpu = $cpu } | ConvertTo-Json -Compress
    Publish-MQTTMessage -Connection $c -Topic "acme/istanbul/it/servers/$env:COMPUTERNAME/telemetry" -Message $json
    Start-Sleep 5
}

Send JSON objects (for example with ConvertTo-Json -Compress): the console turns every numeric field into a live value with a sparkline. Use -Qos 1 or 2 for delivery acknowledgement and -Retain to keep the last value on the broker. Publish an empty message with -Retain to clear a retained message.

Subscribe ​

powershell
# Print every message under acme/istanbul/
Receive-MQTTMessage -Connection $c -Topic 'acme/istanbul/#'

# Wait for one message, then parse it
$msg = Receive-MQTTMessage -Connection $c -Topic 'acme/istanbul/assembly/line1/robot-01/config' -Count 1 -TimeoutSeconds 30
$config = $msg.Payload | ConvertFrom-Json

# Log everything to CSV
Receive-MQTTMessage -Connection $c -Topic 'acme/istanbul/#' |
    Select-Object Received, Topic, Payload |
    Export-Csv mqtt-log.csv -NoTypeInformation

Each message is an object with Topic, Payload (string), RawPayload (bytes), Qos, Retain and Received. -Topic accepts several filters, and + and # wildcards work. Without -Count or -TimeoutSeconds it runs until you press Ctrl+C, and sends keep-alive pings while it waits.

Test without an account ​

powershell
Test-MQTTBroker -Tls                       # connect, publish, receive against the public test broker
$prefix = New-MQTTTopicPrefix              # e.g. connectmq/demo/7f3a91
Receive-MQTTMessage -Topic "$prefix/#" -Tls
Publish-MQTTMessage -Topic "$prefix/temperature" -Message '{"value":21.5}' -Tls

The public test broker needs no account, but anyone can read its topics: use it for experiments only, under a random prefix.

Notes ​

  • The module speaks MQTT 3.1.1 over TCP or TLS only (no WebSockets). If port 8883 is blocked on your network, use a client that supports secure WebSockets on 8084, such as Node.js or Python.
  • The same client is also published as the connectmq.com module (*-ConnectMQ* command names) and as single-file scripts (MQTTBroker-cli, connectmq.com-mqtt) that need no module install.

Fully managed MQTT broker for connected devices.