On this page

REST APIs Requests with PowerShell

Updated on June 21, 2026

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

REST APIs provide a universal interface for interacting with modern systems and services, and PowerShell's Invoke-RestMethod cmdlet is the most efficient way to consume them. It automatically parses JSON and XML responses into PowerShell objects, eliminating the need for manual deserialization. For scenarios requiring detailed HTTP response inspection—such as examining headers, status codes, or cookies—Invoke-WebRequest provides the raw response. For most administrative automation tasks, Invoke-RestMethod is the preferred choice because it converts JSON into structured objects without additional parsing. By mastering these cmdlets, you extend PowerShell beyond the local system and gain the ability to automate cloud services, monitoring platforms, ticketing systems, and virtually any modern application that exposes an HTTP interface.


Understanding REST APIs​

A REST API (Representational State Transfer) is an architectural style for building web services. REST APIs expose resources—such as users, devices, tickets, or configuration objects—through URLs, and they use standard HTTP methods to operate on those resources.

The most common HTTP methods are:

REST APIs typically exchange data in JSON format, which PowerShell handles natively through Invoke-RestMethod.

A REST endpoint might look like:


https://api.example.com/v1/users

PowerShell can interact with this endpoint using built-in cmdlets.


PowerShell Cmdlets for REST Communication​

PowerShell provides two primary cmdlets for working with REST APIs:

For most administrative tasks, Invoke-RestMethod is the preferred choice because it converts JSON into structured objects without additional parsing. Invoke-WebRequest is better suited for scenarios where you need to inspect the raw HTTP response, such as examining headers, status codes, or cookies.


Retrieving Data from a REST API (GET)​

The simplest interaction with a REST API is a GET request.

Example: Retrieving Data​


$response = Invoke-RestMethod -Uri "https://api.example.com/v1/users"

Code explanation: PowerShell sends an HTTP GET request to the API. The API returns JSON data. Invoke-RestMethod converts the JSON into PowerShell objects. $response now contains structured data you can inspect, filter, and manipulate.

For example:


$response[0].name

$response[0].email

This allows you to work with API data as if it were native PowerShell objects.

Using the -UseBasicParsing Parameter​

In Windows PowerShell, the -UseBasicParsing parameter forces the use of a built-in parser instead of relying on Internet Explorer's parsing capabilities. Since Internet Explorer is deprecated and absent from non-Windows operating systems, you should always use this parameter in Windows PowerShell. In PowerShell 7, the -UseBasicParsing parameter has been deprecated—all web requests now use basic parsing by default.


Sending Data to a REST API (POST)​

Many APIs require you to send data in JSON format. PowerShell can convert objects into JSON using ConvertTo-Json.

Example: Creating a New Resource with POST​


$body = @{

    name  = "John"

    email = "[email protected]"

} | ConvertTo-Json



Invoke-RestMethod -Uri "https://api.example.com/v1/users" `

                  -Method Post `

                  -Body $body `

                  -ContentType "application/json"

Code explanation: A PowerShell hashtable is converted into JSON using ConvertTo-Json. The JSON is sent in the request body. The -ContentType "application/json" header tells the API that the request body contains JSON. The API interprets the request as a new user creation. The response typically includes the newly created resource.


Updating Existing Resources (PUT / PATCH)​

REST APIs often allow updates using PUT (replace entire resource) or PATCH (update part of a resource).

Example: Updating a Resource with PATCH​


$update = @{

    email = "[email protected]"

} | ConvertTo-Json



Invoke-RestMethod -Uri "https://api.example.com/v1/users/42" `

                  -Method Patch `

                  -Body $update `

                  -ContentType "application/json"

Code explanation: This updates only the specified field (email) of the user with ID 42. The -Method Patch tells the API to perform a partial update.


Deleting Resources (DELETE)​

To remove a resource, APIs typically use the DELETE method.

Example: Deleting a Resource​


Invoke-RestMethod -Uri "https://api.example.com/v1/users/42" -Method Delete

Code explanation: The API processes the deletion and returns a confirmation or an empty response. No request body is needed for a DELETE operation.


Authentication and Security​

Most APIs require authentication. Common methods include:

API Keys​


Invoke-RestMethod -Uri "https://api.example.com/v1/data" `

                  -Headers @{ "X-API-Key" = "your-key-here" }

Code explanation: The API key is sent in a custom HTTP header. The exact header name depends on the API's design—common names include X-API-Key, Api-Key, or Authorization.

Bearer Tokens (OAuth2)​


Invoke-RestMethod -Uri "https://api.example.com/v1/data" `

                  -Headers @{ Authorization = "Bearer $token" }

Code explanation: The Bearer token is sent in the Authorization header. This is the most common authentication method for modern APIs.

Basic Authentication​


$cred = Get-Credential

Invoke-RestMethod -Uri "https://api.example.com/v1/data" -Credential $cred

Code explanation: Get-Credential prompts for a username and password and returns a PSCredential object. The -Credential parameter handles Basic Authentication by encoding the credentials in Base64 and adding the appropriate Authorization header.

Security Best Practices​


Handling Errors and Responses​

REST APIs return HTTP status codes that indicate success or failure.

Examples:

PowerShell exposes these errors through exceptions.

Example: Handling API Errors​


try {

    Invoke-RestMethod -Uri "https://api.example.com/v1/users/9999"

}

catch {

    Write-Host "The API returned an error:"

    Write-Host $_.Exception.Message

}

Code explanation: The try block attempts the API call. If the API returns an error status code (4xx or 5xx), PowerShell throws an exception. The catch block captures the exception and displays the error message. This allows you to build robust automation that responds gracefully to API failures.

Using -SkipHttpErrorCheck in PowerShell 7​

In PowerShell 7, the -SkipHttpErrorCheck switch parameter suppresses exceptions at the web client level, allowing you to handle errors without try-catch blocks:


$response = Invoke-RestMethod -Uri "https://example.org/NoSuchPageExists" -SkipHttpErrorCheck

if ($response -eq $null) {

    Write-Host "Request failed"

}

Code explanation: When -SkipHttpErrorCheck is used, Invoke-RestMethod does not throw an exception on HTTP errors. Instead, it returns $null, allowing you to check the response manually.


Practical Administrative Scenarios​

Querying a Monitoring System​


$metrics = Invoke-RestMethod -Uri "https://monitoring.example.com/api/cpu"

$metrics.usage

Code explanation: This retrieves CPU usage metrics from a monitoring API. The response is automatically converted to a PowerShell object, allowing direct access to the usage property.

Automating Ticket Creation​


$ticket = @{

    title = "Server outage"

    body  = "The web server is not responding."

} | ConvertTo-Json



Invoke-RestMethod -Uri "https://tickets.example.com/api/issues" `

                  -Method Post `

                  -Body $ticket `

                  -ContentType "application/json"

Code explanation: This creates a new ticket in a ticketing system by sending a POST request with JSON data. The hashtable is converted to JSON and sent as the request body.

Retrieving Cloud Resource Information​


Invoke-RestMethod -Uri "https://cloud.example.com/api/v1/instances" `

                  -Headers @{ Authorization = "Bearer $token" }

Code explanation: This retrieves a list of cloud instances from a cloud provider's API. The Bearer token provides authentication.

Parsing RSS Feeds​

Invoke-RestMethod can also parse RSS and ATOM feeds, returning the Item or Entry XML nodes:


Invoke-RestMethod -Uri "https://devblogs.microsoft.com/powershell/feed/" |

    Format-Table -Property Title, pubDate

Code explanation: This retrieves the PowerShell Team Blog RSS feed and displays the titles and publication dates of recent posts.


Summary​

REST APIs provide a universal interface for interacting with modern systems and services. PowerShell enhances this by offering:

By mastering REST API interactions, you extend PowerShell beyond the local system and gain the ability to automate cloud services, monitoring platforms, ticketing systems, and virtually any modern application that exposes an HTTP interface.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Working with APIs and Web Requests

Next

Invoke-RestMethod vs. Invoke WebRequest