PowerShell Genie

Invoke-RestMethod

Use Invoke-RestMethod to call a REST endpoint and convert the JSON or XML response into PowerShell objects automatically.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Invoke-RestMethod

Syntax

1PS> Invoke-RestMethod [-Uri] <Uri> [-Method <WebRequestMethod>] [-Headers <IDictionary>] [-Body <Object>] [-ContentType <String>] [-Credential <PSCredential>] [-Authentication <WebAuthenticationType>] [-Token <SecureString>] [-TimeoutSec <Int32>] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Invoke-RestMethod [-Uri] <Uri> [-Method <WebRequestMethod>] [-Headers <IDictionary>] [-Body <Object>] [-ContentType <String>] [-Credential <PSCredential>] [-Authentication <WebAuthenticationType>] [-Token <SecureString>] [-TimeoutSec <Int32>] [<CommonParameters>]
  1. Stage 1Invoke-RestMethod-Uri -Method -Headers -Body -ContentType -Credential -Authentication -Token -TimeoutSec

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Invoke-RestMethod -Uri https://api.example.com/items

    1PS> Invoke-RestMethod -Uri https://api.example.com/items -Method Get
    Show reference output

    Sample output · fictional data, shaped like the real thing

    id  name       status
    --  ----       ------
    1   Server01   healthy
    2   Server02   healthy

    Invoke-RestMethod -AsSecureString $h

    1PS> $u = "https://api.example.com/items"
    2PS> $token = Read-Host "Enter API bearer token" -AsSecureString
    3PS> $h = @{ Authorization = "Bearer $token" }
    4PS> Invoke-RestMethod -Uri $u -Headers $h
    Show reference output

    Sample output · fictional data, shaped like the real thing

    id  name       status
    --  ----       ------
    1   Server01   healthy
    2   Server02   healthy

    Invoke-RestMethod -Uri $u

    1PS> $u = "https://api.example.com/items"
    2PS> $o = [pscustomobject]@{ Server = "SRV01"; Status = "Healthy"; Checked = (Get-Date).ToString("o") }
    3PS> Invoke-RestMethod -Uri $u -Method Post -Body ($o | ConvertTo-Json) -ContentType application/json
    Show reference output

    Sample output · fictional data, shaped like the real thing

    {
      "Server": "SRV01",
      "Status": "Healthy",
      "Checked": "2026-09-10T22:31:00+05:30"
    }

    Parameters

    Parameter definitions:

    • -Uri
    • -Method
    • -Headers
    • -Body
    • -ContentType
    • -Credential
    • -Authentication
    • -Token
    • -TimeoutSec

    Invoke-RestMethod on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Invoke-RestMethod when you need to call a REST endpoint and convert the JSON or XML response into PowerShell objects automatically. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: A missing property can reflect a different API response shape. Inspect the returned object before selecting fields, and check pagination instead of assuming one response contains all records. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Invoke-WebRequest. Invoke-WebRequest exposes an HTTP response including headers and content; Invoke-RestMethod is often more convenient when consuming structured API data. Preserve status and headers when troubleshooting the transport. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Invoke-RestMethod reference on Microsoft Learn

    Recap

    Invoke-RestMethod Use Invoke-RestMethod to call a REST endpoint and convert the JSON or XML response into PowerShell objects automatically.

    Next: Invoke-WebRequest
    Genie guideInvoke-RestMethod

    Use Invoke-RestMethod to call a REST endpoint and convert the JSON or XML response into PowerShell objects automatically. The console below is pre-loaded — run it, then change a value and run it again.