Send SMS with PowerShell
To send an SMS from PowerShell, call the curl-sms API with Invoke-RestMethod. The example works in Windows PowerShell 5.1 and PowerShell 7 and keeps umlauts and emoji intact.
Example
function Send-Sms {
param(
[Parameter(Mandatory)] [string] $Receiver,
[Parameter(Mandatory)] [string] $Text
)
$json = @{ receiver = $Receiver; body = $Text } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri 'https://curl-sms.com/api/sms/send' `
-Headers @{ Authorization = "Bearer $env:CURLSMS_KEY" } `
-ContentType 'application/json; charset=utf-8' `
-Body ([System.Text.Encoding]::UTF8.GetBytes($json))
}
$sms = Send-Sms -Receiver '+4915123456789' -Text 'Hello from PowerShell'
"$($sms.id) $($sms.status)"
Set a test key first with $env:CURLSMS_KEY = 'cs_test_…': the API validates everything but sends nothing and charges nothing.
What you get back
Invoke-RestMethod returns the parsed 202 response with id, status, segments and price. On errors it throws; in PowerShell 7 the error JSON is in $_.ErrorDetails.Message:
try { Send-Sms '+4915123456789' 'Test' }
catch { ($_.ErrorDetails.Message | ConvertFrom-Json).error.code }
All codes are listed under error codes.
Tips
- Scheduled tasks: store the key as a machine environment variable or in a secret store, not in the script.
- Idempotent retries: add
'Idempotency-Key' = 'job-42'to-Headers. The same key and body never send twice within 24 hours. - curl on Windows: Windows 10 and 11 ship
curl.exe, so the curl example works too; call it ascurl.exebecausecurlis an alias forInvoke-WebRequestin Windows PowerShell. - Status:
Invoke-RestMethod "https://curl-sms.com/api/sms/$($sms.id)" -Headers @{ Authorization = "Bearer $env:CURLSMS_KEY" }.