PowerShell Genie

Invoke-WebRequest

Use Invoke-WebRequest to fetch a URL and return the full response: status code, headers, and the raw or parsed content.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Invoke-WebRequest

Syntax

1PS> Invoke-WebRequest [-Uri] <Uri> [-Method <WebRequestMethod>] [-Headers <IDictionary>] [-Body <Object>] [-OutFile <String>] [-UseBasicParsing] [-TimeoutSec <Int32>] [-SkipCertificateCheck] [<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-WebRequest [-Uri] <Uri> [-Method <WebRequestMethod>] [-Headers <IDictionary>] [-Body <Object>] [-OutFile <String>] [-UseBasicParsing] [-TimeoutSec <Int32>] [-SkipCertificateCheck] [<CommonParameters>]
  1. Stage 1Invoke-WebRequest-Uri -Method -Headers -Body -OutFile -UseBasicParsing -TimeoutSec -SkipCertificateCheck

    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-WebRequest -Uri https://powershellgenie.com

    1PS> Invoke-WebRequest -Uri https://powershellgenie.com | Select-Object StatusCode, StatusDescription
    Show reference output

    Sample output · fictional data, shaped like the real thing

    StatusCode        : 200
    StatusDescription : OK
    ContentType       : text/html; charset=utf-8
    RawContentLength  : 18422

    Invoke-WebRequest -Uri $u

    1PS> $u = "https://example.com/file.zip"
    2PS> Invoke-WebRequest -Uri $u -OutFile C:\Temp\file.zip
    Show reference output

    Sample output · fictional data, shaped like the real thing

    (No pipeline output. The response body was saved to C:\Temp\file.zip.)

    Invoke-WebRequest -Uri $u (2)

    1PS> $u = "https://powershellgenie.com"
    2PS> try { Invoke-WebRequest -Uri $u -TimeoutSec 10 } catch { $_.Exception.Response.StatusCode }

    Sample output · fictional data, shaped like the real thing

    Version compatibility

    $_.Exception.Response.StatusCode reads the status code on both Windows PowerShell 5.1 and PowerShell 7+: an HTTP error status throws an exception whose .Response is populated on both versions. .Response is $null only when no response arrived at all (DNS failure, connection refused, timeout).

    Show reference output
    StatusCode        : 200
    StatusDescription : OK
    ContentType       : text/html; charset=utf-8
    RawContentLength  : 18422

    Parameters

    Parameter definitions:

    • -Uri
    • -Method
    • -Headers
    • -Body
    • -OutFile
    • -UseBasicParsing
    • -TimeoutSec
    • -SkipCertificateCheck

    Invoke-WebRequest on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Invoke-WebRequest when you need to fetch a URL and return the full response: status code, headers, and the raw or parsed content. 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: HTTP 403 is an authorisation response, not a parsing problem. Check the endpoint permissions, request headers and response body; changing JSON conversion will not grant access. 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-RestMethod. 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-WebRequest reference on Microsoft Learn

    Recap

    Invoke-WebRequest Use Invoke-WebRequest to fetch a URL and return the full response: status code, headers, and the raw or parsed content.

    Next: Invoke-RestMethod
    Genie guideInvoke-WebRequest

    Use Invoke-WebRequest to fetch a URL and return the full response: status code, headers, and the raw or parsed content. The console below is pre-loaded — run it, then change a value and run it again.