PowerShell Invoke-WebRequest [With Examples]

PowerShell Invoke-WebRequest cmdlet allows you to interact with web pages and web services directly from PowerShell by sending HTTP and HTTPS requests. Whether you want to download files, consume APIs, or scrape web content, Invoke-WebRequest can help you do it efficiently.

In this tutorial, we will explain what the Invoke-WebRequest cmdlet is, how to use it, and provide several practical examples to help you learn how to use it.

What is Invoke-WebRequest PowerShell cmdlet?

Invoke-WebRequest is a cmdlet that sends HTTP or HTTPS requests to a web server and retrieves the response. It can parse the response content and extract useful elements such as links, images, forms, and headers.

It was introduced in PowerShell 3.0 and has been improved in later versions, including PowerShell 7.0, where it supports proxy configurations and more.

Check out How to List Running Services in PowerShell

Syntax of Invoke-WebRequest PowerShell cmdlet

Here is the basic syntax of the Invoke-WebRequest cmdlet:

Invoke-WebRequest [-Uri] <string> [-Method <string>] [-Headers <hashtable>] [-Body <string>] [-Credential <PSCredential>] [-Proxy <string>] [-OutFile <string>] [-UseBasicParsing] [<CommonParameters>]
  • -Uri: The URL of the web resource you want to request.
  • -Method: The HTTP method to use, such as GET (default), POST, PUT, DELETE, etc.
  • -Headers: A hashtable of HTTP headers to send with the request.
  • -Body: Data to send with the request, typically used with POST or PUT.
  • -Credential: Credentials for authentication.
  • -Proxy: Proxy server address if needed.
  • -OutFile: Save the response content directly to a file.
  • -UseBasicParsing: Use basic parsing (useful on systems without Internet Explorer).

Read How to List Local Users with PowerShell

PowerShell Invoke-WebRequest Examples

Let’s look at some practical examples that demonstrate how to use Invoke-WebRequest in PowerShell.

1. Simple GET Request to Download a Web Page

The most basic use is to retrieve the HTML content of a web page.

$response = Invoke-WebRequest -Uri "https://www.example.com"
$response.Content

This command sends a GET request to “https://www.example.com” and stores the response in the $response variable. The .Content property contains the HTML source code of the page.

2. Download a File from the Internet

You can download files directly by specifying the URL and output file path using the Invoke-WebRequest PowerShell cmdlet.

Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "C:\Downloads\file.zip"

This downloads the file and saves it locally.

Check out PowerShell Format-List

3. Extract All Links from a Web Page

Since Invoke-WebRequest parses HTML, you can extract links easily.

$response = Invoke-WebRequest -Uri "https://powershellfaqs.com/"
$links = $response.Links | Select-Object -ExpandProperty href
$links

This retrieves all hyperlinks (href attributes) from the page.

You can see the exact output in the screenshot below:

PowerShell Invoke-WebRequest

4. Send a POST Request with Data

You can send data to a web server using the POST method, which is useful for submitting forms or APIs.

Here is an example.

$response = Invoke-WebRequest -Uri "https://api.example.com/login" -Method POST -Body @{username="user"; password="pass"} -ContentType "application/x-www-form-urlencoded"
$response.Content

This sends login credentials as form data.

Read How to List Installed PowerShell Modules

5. Use Custom Headers and Authentication

Sometimes, APIs require headers or authentication tokens.

$headers = @{
    "Authorization" = "Bearer your_access_token"
    "Accept" = "application/json"
}

$response = Invoke-WebRequest -Uri "https://api.example.com/data" -Headers $headers
$response.Content

This example sends a GET request with authorization headers.

Tips for Using Invoke-WebRequest

  • Use -UseBasicParsing if you encounter errors on systems without Internet Explorer.
  • To handle JSON responses, pipe the content to ConvertFrom-Json for easy parsing.
  • Use Invoke-RestMethod as an alternative when working specifically with REST APIs, as it automatically parses JSON.

Conclusion

Invoke-WebRequest in PowerShell helps you to interact with the web directly from PowerShell. You can use this cmdlet to scrape websites, download files, or work with APIs. I hope you now understand how to use the PowerShell Invoke-WebRequest cmdlet.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.