---
title: "How to Query REST APIs with PowerShell: A Practical Guide"
description: "Learn how to use PowerShell to query REST APIs, parse JSON responses, and build a reusable tool for working with web services. This hands-on tutorial covers Invoke-RestMethod, error handling, and creating custom output objects."
canonical: "https://adamtheautomator.com/powershell-rest-api-tutorial/"
---

# How to Query REST APIs with PowerShell: A Practical Guide

> Learn how to use PowerShell to query REST APIs, parse JSON responses, and build a reusable tool for working with web services. This hands-on tutorial covers Invoke-RestMethod, error handling, and creating custom output objects.

Source: https://adamtheautomator.com/powershell-rest-api-tutorial/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Query REST APIs with PowerShell: A Practical Guide](https://adamtheautomator.com/wp-content/uploads/2024/11/image-49.png)

# How to Query REST APIs with PowerShell: A Practical Guide

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)19 November 20242 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [The Basics: Making Your First API Call](#the-basics-making-your-first-api-call)
*   [The Better Way: Using Invoke-RestMethod](#the-better-way-using-invoke-restmethod)
*   [Building a Reusable Tool](#building-a-reusable-tool)
*   [A Note About Error Handling](#a-note-about-error-handling)
*   [Pro Tips](#pro-tips)
*   [Next Steps](#next-steps)

Do you need to integrate external data from web services into your PowerShell scripts? In this practical tutorial, you’ll learn how to query REST APIs using PowerShell and parse the results without any fuss. We’ll build a reusable tool that handles errors gracefully and returns clean, structured data.

Let’s explore this by working with a free API from [postcodes.io](http://postcodes.io/) that provides UK postal code data.

## The Basics: Making Your First API Call

The simplest way to query a REST API with PowerShell is using the `Invoke-WebRequest` cmdlet. Think of it as a command-line web browser – it reaches out to any URL and downloads whatever content is available.

```
$result = Invoke-WebRequest -Uri 'http://api.postcodes.io/random/postcodes'
```

When you run this command, you’ll get back several properties, but the most important ones are:

*   StatusCode – Should be 200 for a successful request
*   Content – The actual data returned by the API

Let’s look at what’s in that Content property:

```
$result.Content
```

![](https://adamtheautomator.com/wp-content/uploads/2024/11/2024-11-17_15-51-17-1024x152.png)

What you’ll see is JSON data containing postal code information. But there’s a catch – this content is just a string:

```
$result.Content.GetType().FullName
# Returns: System.String
```

To actually work with this data in PowerShell, you need to convert it from JSON to objects. Here’s how:

```
$objResult = $result.Content | ConvertFrom-Json
$objResult.result
```

![](https://adamtheautomator.com/wp-content/uploads/2024/11/2024-11-17_15-52-09-1024x475.png)

## The Better Way: Using Invoke-RestMethod

While `Invoke-WebRequest` works, PowerShell has a better cmdlet specifically designed for working with REST APIs: `Invoke-RestMethod`. Here’s the magic – it automatically converts JSON responses into PowerShell objects:

```
$response = Invoke-RestMethod -Uri 'http://api.postcodes.io/random/postcodes'
$response.result
```

Much simpler, right? No manual JSON conversion needed!

## Building a Reusable Tool

Let’s take this to the next level and build a reusable script. We’ll create a tool that can:

*   Accept different API endpoints
*   Handle errors gracefully
*   Return nicely formatted results

Here’s our script (save it as Get-RandomPostCode.ps1):

```
[CmdletBinding()]
param(
    [Parameter()]
    [string]$Uri = 'http://api.postcodes.io/random/postcodes'
)

$response = Invoke-RestMethod -Uri $Uri -SkipHttpErrorCheck -StatusCodeVariable respStatus
if ($respStatus -ne 200) {
    throw "API query failed: $_"
} else {
    [PSCustomObject]@{
        Postcode = $response.result.postcode
        Country  = $response.result.country
        Region   = $response.result.region
        Latitude = $response.result.latitude
        Longitude = $response.result.longitude
        JSONFullResponse = $response
    }
}
```

Let’s break down what this script does:

1.  Accepts a custom URI parameter with a default value
2.  Uses -SkipHttpErrorCheck and -StatusCodeVariable for better error handling
3.  Returns a clean custom object with just the properties we care about
4.  Includes the full JSON response if needed for troubleshooting

To use the script:

```
# Get a random postcode using the default API endpoint
.\Get-RandomPostCode.ps1

# Try a custom endpoint (this will demonstrate error handling)
.\Get-RandomPostCode.ps1 -Uri 'https://oops.postcodes.io'
```

## A Note About Error Handling

In PowerShell 7+, we have some nice features for handling API errors. The `-SkipHttpErrorCheck` parameter tells PowerShell not to throw errors into the error stream (avoiding that scary red text), while `-StatusCodeVariable` lets us check the HTTP status code ourselves.

If you’re using an older version of PowerShell, you can use this alternative error handling approach:

```
$ErrorActionPreference = 'Stop'
try {
    $response = Invoke-RestMethod -Uri $Uri
    $response.result
} catch {
    throw "API query failed: $_"
}
```

## Pro Tips

1.  Always check the status code – just because an API responded doesn’t mean it worked
2.  Consider returning custom objects instead of raw API responses – it makes your scripts more professional and easier to work with
3.  Include the full API response in your output objects for troubleshooting

## Next Steps

Now that you know how to work with REST APIs in PowerShell, you can:

*   Integrate data from any web service into your scripts
*   Build wrapper functions for your favorite APIs
*   Create tools that combine data from multiple sources

The possibilities are endless! Just remember – always check the API’s documentation for authentication requirements and rate limits before building production tools.

What APIs are you planning to integrate with PowerShell? Let me know in the comments!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-rest-api-tutorial%2F&text=How%20to%20Query%20REST%20APIs%20with%20PowerShell%3A%20A%20Practical%20Guide)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-rest-api-tutorial%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-rest-api-tutorial%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
