Skip to main content

How to Send a GET Request with Invoke-RestMethod in PowerShell

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Invoke-RestMethod followed by a web address sends a GET request and returns the answer. When the answer is JSON, PowerShell turns it into objects you can read. This guide asks a small web server on your own computer for a JSON file, prints the answer and reads values from it, with real output from PowerShell 7.6.10.

The Invoke-RestMethod page: "The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data."

Syntax​

Syntax
Invoke-RestMethod <URL>
$<name> = Invoke-RestMethod -Uri <URL>
PartWhat it means
Invoke-RestMethodSends the request and returns the answer
<URL>The web address to ask, such as http://127.0.0.1:8023/products.json
-UriThe name of the parameter that takes the address. You can leave it out
$<name> =Optional: saves the answer in a variable instead of printing it

The page on -Uri: "This parameter is required. The parameter name (Uri) is optional."

The Example​

The example asks a small web server on your own computer for products.json, a file that lists two products. The web server that comes with Python serves the file, so the request never leaves your computer. The commands run in PowerShell 7.6.10 on a Mac, in a folder named rest-get-demo.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.
  • Python 3, for the example's web server: python3 --version printed Python 3.15.2.

Step 1: Save the JSON File​

In the rest-get-demo folder, save this text as products.json:

products.json
[
{ "name": "Notebook", "price": 4.5, "inStock": true },
{ "name": "Pen", "price": 1.2, "inStock": false }
]

The square brackets hold a list, and each pair of braces inside it is one product with three values: a name, a price and whether it's in stock.

Step 2: Start the Web Server​

Open a second PowerShell window, move to the rest-get-demo folder, and start the server:

PowerShell (second window)
python3 -m http.server 8023 --bind 127.0.0.1
Output
Serving HTTP on 127.0.0.1 port 8023 (http://127.0.0.1:8023/) ...

python3 -m http.server starts the web server that comes with Python, which serves the files of the current folder. Its own help, python3 -m http.server --help, describes the two values: 8023 is the port, "bind to this port (default: 8000)", and --bind 127.0.0.1 sets the address, "bind to this address (default: all interfaces)". 127.0.0.1 is the address a computer uses for itself, so only programs on this computer can reach the server. Leave this window open: the server runs until Step 5 stops it.

Step 3: Send the GET Request​

Back in the first window, send the request:

PowerShell
Invoke-RestMethod http://127.0.0.1:8023/products.json
Output
name price inStock
---- ----- -------
Notebook 4.500 True
Pen 1.200 False

macOS-style terminal window titled PowerShell where Invoke-RestMethod asks http://127.0.0.1:8023/products.json and prints a table with the columns name, price and inStock: Notebook, 4.500, True and Pen, 1.200, False.

Invoke-RestMethod asked the server for products.json, and PowerShell turned the JSON it got back into objects, one per product, which the console printed as a table. The page: "For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into [pscustomobject] objects." The table shows each price with three decimal places, but the values didn't change: Step 4 reads the pen's price as 1.2. The zeros come from the table view, which gives every decimal number as many places as the culture's NumberDecimalDigits setting, as PowerShell's own Format-Table tests check. In this console, (Get-Culture).NumberFormat.NumberDecimalDigits returned 3.

The second window logged the request:

Output
Serving HTTP on 127.0.0.1 port 8023 (http://127.0.0.1:8023/) ...
127.0.0.1 - - [03/Dec/2026 16:52:23] "GET /products.json HTTP/1.1" 200 -

The command named no method, and the server received a GET request for /products.json. It answered with the status code 200, which means success.

Step 4: Save the Answer and Read Its Values​

PowerShell
$products = Invoke-RestMethod -Uri http://127.0.0.1:8023/products.json
$products.Count
$products[0].name
$products[1].price

macOS-style code window titled PowerShell where Invoke-RestMethod with the Uri parameter saves the answer in $products, and the next three lines read its Count, the name of the first product and the price of the second.

Output
2
Notebook
1.2

With $products = in front, the answer went into the variable instead of onto the screen. The JSON held a list of two products, so $products holds two objects: .Count counted them, [0] picked the first and [1] the second, and the name after the dot read one value. The page: "When the REST endpoint returns multiple objects, the objects are received as an array." This time the command named the -Uri parameter, with the same result.

Step 5: Stop the Web Server​

In the second window, press Ctrl+C:

Output
^C
Keyboard interrupt received, exiting.

The server stopped and the prompt came back. When you no longer need the example, delete the rest-get-demo folder.

Common Mistakes​

MistakeWhat happensDo this instead
Sending the request after the server stoppedInvoke-RestMethod: Connection refusedStart the server again, as in Step 2, and leave its window open
A misspelled file name: http://127.0.0.1:8023/product.jsonInvoke-RestMethod: followed by the server's error page, with Error code: 404 and Message: File not found.Check the address: the file is products.json
Piping the answer: (Invoke-RestMethod http://127.0.0.1:8023/products.json | Measure-Object).Count1: the list of two products reached Measure-Object as one itemPut the request in parentheses first: ((Invoke-RestMethod http://127.0.0.1:8023/products.json) | Measure-Object).Count printed 2

The Invoke-RestMethod page explains the last row: "If you pipe the output from Invoke-RestMethod to another command, it's sent as a single [Object[]] object." And: "To enumerate the objects into the pipeline, pipe the results to Write-Output or wrap the cmdlet in parentheses."

FAQ​

Is irm the same command?​

Yes. irm is a short name for it: irm http://127.0.0.1:8023/products.json printed the same table. The page: "PowerShell includes the following aliases for Invoke-RestMethod:", and the alias it lists is irm.

Do I need -Method Get?​

No. Without -Method, the server received a GET request, as its log in Step 3 shows, and Invoke-RestMethod http://127.0.0.1:8023/products.json -Method Get printed the same table.

How is Invoke-WebRequest different?​

It returns a response object instead of converting the JSON: (Invoke-WebRequest http://127.0.0.1:8023/products.json).Content printed the text of products.json exactly as it is in the file. The Invoke-WebRequest page: "The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service." Use Invoke-RestMethod when you want the values as objects.

Conclusion​

Invoke-RestMethod http://127.0.0.1:8023/products.json sent a GET request and returned the two products as objects. Save the answer in a variable to read its values, put the request in parentheses before you pipe a list, and stop the test server with Ctrl+C when you're done.

Additional Resources​

  • Invoke-RestMethod — every parameter, including Uri and Method, with examples.
  • Invoke-WebRequest — the command that returns a response object, with the text in its Content property.