Sleep or Pause to PowerShell Script

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
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.