0
0
PowerShellscripting~10 mins

REST API calls with Invoke-RestMethod in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - REST API calls with Invoke-RestMethod
Start Script
Prepare API URL
Call Invoke-RestMethod
Receive Response
Process/Display Data
End Script
This flow shows how a PowerShell script prepares a URL, calls Invoke-RestMethod to get data from a REST API, then processes and shows the response.
Execution Sample
PowerShell
$url = 'https://api.agify.io?name=michael'
$response = Invoke-RestMethod -Uri $url
$response | Format-List
This script calls a public API to guess the age for the name 'michael' and shows the response details.
Execution Table
StepActionVariable/CommandResult/Output
1Set API URL$url = 'https://api.agify.io?name=michael'Variable $url holds the API endpoint string
2Call REST API$response = Invoke-RestMethod -Uri $urlStores JSON response as PowerShell object in $response
3Display response$response | Format-ListShows properties: name, age, count with values
4EndScript finishesNo further actions
💡 Script ends after displaying the API response
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$urlnull'https://api.agify.io?name=michael''https://api.agify.io?name=michael''https://api.agify.io?name=michael'
$responsenullnull{name: 'michael', age: 69, count: 12345}{name: 'michael', age: 69, count: 12345}
Key Moments - 3 Insights
Why do we assign the API URL to a variable before calling Invoke-RestMethod?
Assigning the URL to $url makes the script easier to read and change. The execution_table row 1 shows $url holding the API endpoint before the call.
What type of data does Invoke-RestMethod return and how is it stored?
Invoke-RestMethod converts JSON response into a PowerShell object stored in $response, as shown in execution_table row 2.
Why do we use Format-List when displaying $response?
Format-List shows all properties clearly in a list format, making it easier to read the API response, as in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $url after step 1?
A'https://api.agify.io?name=michael'
Bnull
C'https://api.agify.io?name=john'
DAn empty string
💡 Hint
Check the 'Variable/Command' and 'Result/Output' columns for step 1 in execution_table
At which step does $response get its value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Variable/Command' columns in execution_table to see when $response is assigned
If we changed the URL to a different name, how would $response change?
AIt would stay the same
BIt would be empty
CIt would contain data for the new name
DIt would cause an error
💡 Hint
Refer to variable_tracker showing $response content depends on the API URL value
Concept Snapshot
Invoke-RestMethod calls a REST API and returns data as a PowerShell object.
Assign the API URL to a variable for clarity.
Store the response in a variable to use later.
Use Format-List or other commands to display the data.
This method works for GET requests easily.
Full Transcript
This example shows how to use PowerShell's Invoke-RestMethod to call a REST API. First, the API URL is saved in the variable $url. Then Invoke-RestMethod calls the API and stores the JSON response as a PowerShell object in $response. Finally, Format-List displays the response properties clearly. This process helps you get and use data from web APIs simply in scripts.