Windows

Hey there! If you‘ve ever needed to make HTTP requests from PowerShell scripts, you‘re probably familiar with the handy Invoke-WebRequest cmdlet. But what if you need to route those requests through a proxy server? Never fear – using Invoke-WebRequest with a proxy is easier than you might think!

In this guide, I‘ll walk you through everything you need to know to become a PowerShell proxy pro. We‘ll cover what Invoke-WebRequest is, how to install it, and the prerequisites for specifying a proxy. Then I‘ll provide step-by-step instructions for using an HTTP proxy, as well as tips for dealing with HTTPS and SOCKS proxies in PowerShell 7.x and up.

By the end, you‘ll be confidently making web requests via proxy without breaking a sweat. So grab a cup of coffee, fire up your PowerShell console, and let‘s get started!

What is Invoke-WebRequest?

First things first – what exactly is this Invoke-WebRequest thing we‘re talking about? In short, it‘s a PowerShell cmdlet that allows you to send HTTP/HTTPS requests to a web server and receive the response. You can think of it like a command-line version of your web browser.

With Invoke-WebRequest, you can:

  • Make GET, POST, PUT, and DELETE requests
  • Add custom headers
  • Include a request body
  • Parse the response data
  • Download files
  • Interact with REST APIs and web services

The basic syntax looks like this:


Invoke-WebRequest [-Uri] [-Method ] [-Headers ] [-Body ]

The only mandatory parameter is the URI of the web resource you want to access. By default, Invoke-WebRequest will make a GET request if you don‘t specify a method.

This cmdlet has been part of PowerShell since version 3.0, so chances are it‘s already available on your system. But let‘s cover installation real quick just in case.

Installing Invoke-WebRequest

How you install PowerShell and get access to Invoke-WebRequest depends on your operating system:

Windows

On modern versions of Windows, PowerShell 5.1 comes preinstalled. This release includes the Invoke-WebRequest cmdlet, so you‘re good to go!

If you have an older Windows version, you may need to install PowerShell separately. Follow Microsoft‘s installation instructions for your release.

Note that some newer Invoke-WebRequest features are only available in PowerShell 7.x and later. You can install 7.x alongside 5.1 if needed. Use this command to check your current version:


$PSVersionTable

macOS and Linux

While you can install PowerShell 7.x on macOS and Linux, it‘s usually overkill just to get Invoke-WebRequest. Instead, you can use the curl command which comes standard and provides the same functionality. Check out our curl proxy guide for more info.

Proxy Prerequisites

Before we dive into the code, let‘s make sure you understand the structure of a proxy server URL. You‘ll need to provide this to Invoke-WebRequest in order to route requests through the proxy.

Here‘s the format:

://[:@][:]

Breaking that down:

  • – The protocol used to communicate with the proxy. Usually HTTP, HTTPS, or SOCKS.
  • – The hostname or IP address of the proxy server.
  • – The port the proxy is listening on.
  • – The username for proxy authentication (optional).
  • – The password for proxy authentication (optional).

The :// part is required, otherwise you‘ll get an error like:


Invoke-WebRequest : This operation is not supported for a relative URI.

For this guide, we‘ll use a free HTTP proxy as an example. But keep in mind that free proxies are not reliable or secure enough for real-world usage. I highly recommend using premium proxies from a reputable provider like Bright Data. More on that later.

Here‘s a sample free proxy:


Protocol: HTTP
IP Address: 190.6.23.219
Port: 999

The corresponding proxy URL would be:


http://190.6.23.219:999

If your proxy requires authentication, it might look more like:


http://username:[email protected]:999

With that background in place, let‘s look at how to actually use the proxy in Invoke-WebRequest!

Using an HTTP Proxy

There are two ways to specify a proxy for Invoke-WebRequest: via command line options or environment variables. We‘ll walk through both.

Command Line Options

The -Proxy parameter allows you to set the proxy URL on a per-request basis. Just provide the full proxy URL as the parameter value.

For example, let‘s make a request to https://httpbin.org/ip which returns the IP address Invoke-WebRequest appears to come from.

Normally (without a proxy) it will return our own public IP:


Invoke-WebRequest "https://httpbin.org/ip"

StatusCode : 200
Content : {
"origin": "203.45.6.8"
}
...

But if we add our example proxy:


Invoke-WebRequest -Proxy "http://190.6.23.219:999" "https://httpbin.org/ip"

StatusCode : 200
Content : {
"origin": "190.6.23.219"
}
...

The request now shows as coming from the proxy IP instead of ours. Easy enough!

One caveat – free proxies like this example rarely stay up for long. So don‘t be surprised if you get errors trying to use this later on. Just substitute a different proxy URL.

Environment Variables

Starting in PowerShell 7.0, you can also configure proxies via environment variables. There are two relevant variables:

  • HTTP_PROXY – The URL of the proxy to use for HTTP requests
  • HTTPS_PROXY – The URL of the proxy to use for HTTPS requests

You can set them like this:

$env:HTTP_PROXY = "http://190.6.23.219:999"
$env:HTTPS_PROXY = "http://190.6.23.219:999"

export HTTP_PROXY="http://190.6.23.219:999"
export HTTPS_PROXY="http://190.6.23.219:999"

Now any Invoke-WebRequest calls will automatically use the specified proxy without needing the -Proxy option.

To revert back to direct requests, simply clear the variables:

$env:HTTP_PROXY = ""
$env:HTTPS_PROXY = ""

unset HTTP_PROXY
unset HTTPS_PROXY

HTTPS and SOCKS Proxies

So far we‘ve focused on HTTP proxies, but what about HTTPS and SOCKS? The good news is the syntax is exactly the same:


Invoke-WebRequest -Proxy "://[:@][:]"

Just use https, socks4, socks4a, socks5, or socks5a for the part.

The catch is this only works in PowerShell 7.x and later. In earlier versions you‘ll get an error like:


The ServicePointManager does not support proxies with the https scheme.

Assuming you are on 7.x+, an example SOCKS5 proxy request would look like:


Invoke-WebRequest -Proxy "socks5://192.165.100.8:3517" "https://httpbin.org/ip"

Tips and Tricks

Here are a few bonus tips to level up your PowerShell proxy skills:

Bypassing Proxy Configuration

To make a request that ignores any configured proxy (e.g. from environment variables), add the -NoProxy switch:


Invoke-WebRequest -NoProxy "https://httpbin.org/ip"

Avoiding SSL Errors

Proxied HTTPS requests may encounter SSL certificate errors. To suppress those, use -SkipCertificateCheck:


Invoke-WebRequest -SkipCertificateCheck -Proxy "..." "https://example.com"

Warning: This allows insecure connections. Only use with trusted sites.

Choosing the Right Proxy

I mentioned earlier that free proxies are a bad idea for anything beyond basic testing and learning. They tend to be unreliable, slow, and can even steal your data.

So what should you use instead? It depends on your specific needs, but here are the main types of proxies to consider:

  • Datacenter proxies – fast and cheap, but easily blocked due to obvious IP ranges
  • Residential proxies – rotating IPs from real home devices for better anonymity
  • ISP proxies – static IPs registered to ISPs, ideal for SEO monitoring and market research
  • Mobile proxies – IPs from real mobile carrier devices

When in doubt, residential proxies are usually the best all-around choice for their combination of anonymity, reliability and performance.

Bright Data – The Best Proxy Solution

I‘ve tested many proxy providers over the years, and the clear winner is Bright Data. They offer the largest, most reliable proxy network including all the types mentioned above at massive scale:

  • 770,000+ datacenter IPs
  • 72+ million residential IPs across 195 countries
  • 700,000+ ISP IPs
  • 7+ million mobile IPs

Bright Data‘s proxies are used by Fortune 500 companies, "Big Tech", and over 20,000 other customers for web scraping, SEO, brand protection and more. Unlike free proxies, their network is fast, secure and built for high-volume commercial use cases.

Best of all, you can test the proxies for free before committing to a plan. Just sign up for a trial account to get started.

Summary

And there you have it – a complete guide to using Invoke-WebRequest with proxies in PowerShell! We covered a lot of ground, so let‘s recap the key takeaways:

  • Invoke-WebRequest is a cmdlet for making HTTP requests from PowerShell
  • You can route requests through a proxy for anonymity, geo-unblocking etc.
  • The proxy URL format is ://[:@][:]
  • Set the proxy per-request with -Proxy or globally with HTTP_PROXY/HTTPS_PROXY environment variables
  • Free proxies are tempting for testing but not reliable or secure
  • Bright Data offers the best premium proxies for any use case

Hopefully this guide has given you the knowledge and confidence to tackle proxy scenarios in your PowerShell scripting. It really is simpler than it seems once you understand the basics.

Happy proxying!

Similar Posts