Last Updated on August 7, 2025 by Arnav Sharma
The Invoke-RestMethod cmdlet in PowerShell is used to send HTTP and HTTPS requests to RESTful web services. It is a versatile cmdlet that can be used to interact with REST APIs, making it possible to send GET, POST, PUT, DELETE, and other types of HTTP requests.
Here’s a detailed explanation of Invoke-RestMethod with examples:
Syntax
Invoke-RestMethod
[-Method <WebRequestMethod>]
[-Uri] <Uri>
[-Body <Object>]
[-Headers <IDictionary>]
[-ContentType <String>]
[-Credential <PSCredential>]
[-Authentication <WebAuthenticationType>]
[-CertificateThumbprint <String>]
[-Certificate <X509Certificate>]
[-TimeoutSec <Int32>]
[<CommonParameters>]
Parameters
- -Method: Specifies the HTTP method to use (GET, POST, PUT, DELETE, etc.). Default is GET.
- -Uri: Specifies the URI of the RESTful web service.
- -Body: Specifies the data to send to the web service. This is often used with POST and PUT requests.
- -Headers: Specifies custom headers for the web request as a hashtable.
- -ContentType: Specifies the content type of the request body, such as
application/json. - -Credential: Specifies the user credentials to use for the web service.
- -Authentication: Specifies the authentication type to use.
- -CertificateThumbprint: Specifies the thumbprint of a certificate to use for the request.
- -Certificate: Specifies a certificate to use for the request.
- -TimeoutSec: Specifies the timeout in seconds for the web request.
Examples:
Example 1: GET Request
This example retrieves data from a REST API using a GET request.
$uri = "https://jsonplaceholder.typicode.com/posts/1"
$response = Invoke-RestMethod -Uri $uri -Method GET
$response
Example 2: POST Request with JSON Body
This example sends data to a REST API using a POST request.
$uri = "https://jsonplaceholder.typicode.com/posts"
$body = @{
title = "foo"
body = "bar"
userId = 1
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri $uri -Method POST -Body $body -ContentType "application/json"
$response
Example 3: POST Request with Headers
This example sends data to a REST API using a POST request and includes custom headers.
$uri = "https://jsonplaceholder.typicode.com/posts"
$body = @{
title = "foo"
body = "bar"
userId = 1
} | ConvertTo-Json
$headers = @{
Authorization = "Bearer YOUR_ACCESS_TOKEN"
"Custom-Header" = "HeaderValue"
}
$response = Invoke-RestMethod -Uri $uri -Method POST -Body $body -ContentType "application/json" -Headers $headers
$response
Example 4: PUT Request
This example updates data on a REST API using a PUT request.
$uri = "https://jsonplaceholder.typicode.com/posts/1"
$body = @{
id = 1
title = "foo"
body = "bar"
userId = 1
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri $uri -Method PUT -Body $body -ContentType "application/json"
$response
Example 5: DELETE Request
This example deletes data from a REST API using a DELETE request.
$uri = "https://jsonplaceholder.typicode.com/posts/1"
$response = Invoke-RestMethod -Uri $uri -Method DELETE
$response
Explanation of Responses
Invoke-RestMethod automatically converts JSON and XML responses into PowerShell objects, making it easy to work with the data. For example, if the response is in JSON format, PowerShell will convert it into a PSCustomObject that you can access using dot notation.
Error Handling
To handle errors, you can use try-catch blocks. For example:
try {
$uri = "https://jsonplaceholder.typicode.com/posts/1"
$response = Invoke-RestMethod -Uri $uri -Method GET
$response
}
catch {
Write-Error $_.Exception.Message
}
I help organisations secure their cloud infrastructure and stay ahead of evolving cyber threats. Microsoft MVP and Certified Trainer, author of Mastering Azure Security, and founder of arnav.au — a platform for practical Cloud, Cybersecurity, DevOps and AI content.
Frequently Asked Questions
The default HTTP method is GET. If you don't specify the -Method parameter, Invoke-RestMethod will automatically use GET to retrieve data from the specified URI.
Invoke-RestMethod automatically converts JSON responses into PowerShell objects, specifically PSCustomObject. This allows you to easily access and manipulate the data using dot notation without manually parsing the JSON.
Use the -Body parameter to specify the data you want to send in POST or PUT requests. You typically need to convert your data to JSON format using ConvertTo-Json before passing it to the -Body parameter, along with setting -ContentType to 'application/json'.
Use the -Headers parameter, which accepts a hashtable of custom headers. You can include authentication tokens like 'Authorization: Bearer YOUR_ACCESS_TOKEN' or any other custom headers your API requires in this hashtable.
The recommended approach is to use try-catch blocks to handle potential errors. In the catch block, you can access error details using $_.Exception.Message and use Write-Error or other logging methods to handle the error appropriately.