Invoke-WebRequest
Use Invoke-WebRequest to fetch a URL and return the full response: status code, headers, and the raw or parsed content.
Documentation: Microsoft Learn — Invoke-WebRequest
Syntax
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>]- Stage 1Invoke-WebRequest
-Uri -Method -Headers -Body -OutFile -UseBasicParsing -TimeoutSec -SkipCertificateCheck
Try it
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.
Follow the objects
Run the command to inspect each stage.
Examples
Invoke-WebRequest -Uri https://powershellgenie.com
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
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)
Sample output · fictional data, shaped like the real thing
$_.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
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.
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