Convert curl to PowerShell
Convert curl commands to PowerShell Invoke-WebRequest and Invoke-RestMethod. Translate headers, authentication, and body parameters for Windows users.
PowerShell
Detailed Explanation
Converting curl to PowerShell
PowerShell provides two cmdlets for HTTP requests: Invoke-WebRequest (full response with headers) and Invoke-RestMethod (auto-parsed response body). Both serve as PowerShell equivalents to curl.
GET Request
curl:
curl https://api.example.com/users
PowerShell:
$response = Invoke-RestMethod -Uri "https://api.example.com/users"
# $response is already parsed as a PowerShell object
POST with JSON
curl:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice"}'
PowerShell:
$body = @{ name = "Alice" } | ConvertTo-Json
$response = Invoke-RestMethod -Uri "https://api.example.com/users" `
-Method POST `
-ContentType "application/json" `
-Body $body
Custom Headers
curl:
curl -H "Authorization: Bearer token123" \
-H "X-Custom: value" \
https://api.example.com/me
PowerShell:
$headers = @{
"Authorization" = "Bearer token123"
"X-Custom" = "value"
}
$response = Invoke-RestMethod -Uri "https://api.example.com/me" -Headers $headers
Basic Authentication
curl:
curl -u user:pass https://api.example.com/data
PowerShell:
$cred = [PSCredential]::new("user", (ConvertTo-SecureString "pass" -AsPlainText -Force))
$response = Invoke-RestMethod -Uri "https://api.example.com/data" -Credential $cred
File Download
curl:
curl -o file.zip https://example.com/file.zip
PowerShell:
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"
Key Mapping
| curl flag | PowerShell parameter |
|---|---|
-X METHOD |
-Method METHOD |
-H |
-Headers @{} |
-d |
-Body |
-u |
-Credential |
-o |
-OutFile |
-k |
-SkipCertificateCheck |
-L |
-MaximumRedirection (default: 5) |
Note on PowerShell curl Alias
In PowerShell 5.x, curl is an alias for Invoke-WebRequest. To use actual curl, call curl.exe explicitly. PowerShell 7+ removed this alias to avoid confusion.
Use Case
A Windows system administrator needs to convert curl-based API examples from documentation into PowerShell scripts for automation tasks in a Windows environment.