Routing Invoke-WebRequest and Invoke-RestMethod Through an Authenticated Proxy

· Updated:

To send a PowerShell Invoke-WebRequest proxy request with a login, pass the proxy address to -Proxy and a PSCredential to -ProxyCredential; Invoke-RestMethod takes the same two parameters. What changes between Windows PowerShell 5.1 and PowerShell 7 is everything around that call: where the default proxy comes from, whether HTTP_PROXY is honored, and whether SOCKS5 works at all.

Windows automation with a fixed exit IP

Two PowerShells, two networking stacks

Windows PowerShell 5.1 ships with Windows and runs on .NET Framework, so its web cmdlets sit on HttpWebRequest. PowerShell 7 runs on modern .NET and uses HttpClient. The parameter names are identical, which hides the fact that the default proxy behavior is not. Check which one you are in with $PSVersionTable.PSVersion before you debug anything else.

The table sums up the differences that matter for proxy work.

BehaviorWindows PowerShell 5.1PowerShell 7.x
Default proxy when -Proxy is omitted[System.Net.WebRequest]::DefaultWebProxy, which mirrors the Windows (WinINET) user settings[System.Net.Http.HttpClient]::DefaultProxy: environment variables first, then the user's system settings on Windows
Reads HTTP_PROXY / HTTPS_PROXY / NO_PROXYNoYes, on Windows, macOS and Linux
-NoProxy switchNot availableAvailable (added in 6.0)
SOCKS5 proxy URINot supportedAccepted on current releases that run on .NET 6 or later
Credentials inside the proxy URLIgnoredHonored when they come from HTTP_PROXY; use -ProxyCredential with -Proxy

Creating the credential object safely

-ProxyCredential wants a PSCredential, not a plain string. How you create it depends on whether a person is at the keyboard.

Interactive sessions

Get-Credential opens a prompt and returns the object, so the password never lands in your command history.

$cred = Get-Credential -Message 'Proxy login' -UserName 'USER'

Scheduled tasks and CI jobs

Unattended scripts cannot answer a prompt. Read the password from a secret store or a protected environment variable and convert it. The Microsoft.PowerShell.SecretManagement module is the tidy option on shared machines; a masked variable injected by your CI runner works for pipeline agents.

$secure = ConvertTo-SecureString $env:PROXY_PASS -AsPlainText -Force
$cred   = [pscredential]::new('USER', $secure)

Per-request proxy with -Proxy and -ProxyCredential

Give the proxy as a URI with the http:// scheme, even when the target site is HTTPS. PowerShell opens a CONNECT tunnel through the proxy and the TLS session runs end to end with the site, so the proxy only sees the hostname.

$proxy = 'http://203.0.113.10:8080'

# Full response object (status, headers, content)
$r = Invoke-WebRequest -Uri 'https://api.ipify.org?format=json' `
     -Proxy $proxy -ProxyCredential $cred -UseBasicParsing
$r.Content

# Parsed JSON straight away
Invoke-RestMethod -Uri 'https://api.ipify.org?format=json' `
     -Proxy $proxy -ProxyCredential $cred

-UseBasicParsing matters only on 5.1, where the default parser depends on Internet Explorer components; PowerShell 7 accepts the switch and ignores it. Both credential parameters are valid only together with -Proxy, and PowerShell refuses a command that combines -ProxyCredential with -ProxyUseDefaultCredentials.

-ProxyUseDefaultCredentials sends the logged-on Windows account to the proxy through NTLM or Kerberos. That is what corporate gateways expect. A paid proxy with its own username and password will not recognize your Windows login, so it answers 407 and you should use -ProxyCredential instead.

Setting a PowerShell default proxy for the whole session

Repeating -Proxy on every call gets noisy in long scripts. Each edition has its own way to set a default once, plus one trick that works in both.

Windows PowerShell 5.1: DefaultWebProxy

Replace the process-wide default and every web cmdlet call that omits -Proxy will use it. The change lasts until the PowerShell window closes.

$wp = New-Object System.Net.WebProxy('http://203.0.113.10:8080', $true)
$wp.Credentials = $cred.GetNetworkCredential()
[System.Net.WebRequest]::DefaultWebProxy = $wp
Invoke-RestMethod 'https://api.ipify.org?format=json'

The second constructor argument, $true, bypasses the proxy for local addresses. If you sit behind a corporate proxy that already works in the browser and only needs your Windows login, the shorter fix is [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials.

PowerShell 7: environment variables

PowerShell 7 derives HttpClient.DefaultProxy from HTTP_PROXY, HTTPS_PROXY, ALL_PROXY and NO_PROXY, and it accepts credentials inside the URL. The default is read once per process, so set the variables before the first web request, or open a new pwsh window after setting them.

$env:HTTP_PROXY  = 'http://USER:[email protected]:8080'
$env:HTTPS_PROXY = $env:HTTP_PROXY
$env:NO_PROXY    = 'localhost,127.0.0.1,.internal.example'
Invoke-RestMethod 'https://api.ipify.org?format=json'

Percent-encode reserved characters in the password (@ becomes %40, : becomes %3A), or the URL parses wrong. The variables also steer other tools started from the same session, such as Git and Python, which can be convenient or surprising. To make them permanent for your account, the Windows proxy setup page shows the SetEnvironmentVariable approach and the WinHTTP layer that services use.

Both editions: $PSDefaultParameterValues

PowerShell can inject parameter values automatically. This keeps the credential as a PSCredential rather than a plain string and behaves the same in 5.1 and 7.

$p = 'http://203.0.113.10:8080'
$PSDefaultParameterValues['Invoke-WebRequest:Proxy']           = $p
$PSDefaultParameterValues['Invoke-WebRequest:ProxyCredential'] = $cred
$PSDefaultParameterValues['Invoke-RestMethod:Proxy']           = $p
$PSDefaultParameterValues['Invoke-RestMethod:ProxyCredential'] = $cred

SOCKS5 from PowerShell

Windows PowerShell 5.1 has no SOCKS client; HttpWebRequest only talks to HTTP proxies. In current PowerShell 7 releases the underlying .NET handler understands socks5:// URIs, so -Proxy 'socks5://203.0.113.10:1080' with -ProxyCredential is worth a try when a proxy offers SOCKS only. Older 7.x releases reject the scheme, so upgrade if you get an unsupported-proxy error.

In most PowerShell automation there is no reason to prefer SOCKS5. HTTPS traffic is encrypted end to end either way, as the HTTP vs SOCKS5 comparison explains, and the HTTP mode works in every PowerShell version.

When a fixed exit IP makes Windows automation simpler

Many PowerShell jobs call APIs that restrict access to a known source address: partner endpoints, payment dashboards, cloud consoles with network rules, regional storefront APIs. A rotating or shared address breaks those rules every time it changes. A ProxyV4 private IPv4 or static ISP proxy has a single tenant for the whole term and keeps the same address when you renew before expiry, so the address you register once stays valid.

Each proxy arrives as one ip:port:user:pass line and supports HTTP(S) and SOCKS5 with username and password, so it maps directly onto -Proxy and -ProxyCredential. Traffic on private IPv4 and static ISP is not metered, which suits scheduled jobs that pull large exports. Datacenter IPv4 covers the United States, Vietnam, Japan, South Korea, Singapore, the United Kingdom and Hong Kong, with static ISP in the US and Vietnam.

It is the wrong tool if you need a new address on every request; rotating proxies are not on sale. Start with a single IP, run it through the proxy checker and your real script, then add more from the live prices.

Errors and what they usually mean

407 Proxy Authentication Required

The proxy rejected or never received the login. Typical causes are -ProxyUseDefaultCredentials against a password-protected proxy, credentials embedded in -Proxy on 5.1 where they are dropped, or an unencoded special character in HTTP_PROXY. The 407 error guide walks through the rest.

"The underlying connection was closed" on 5.1

Older 5.1 installs may not offer TLS 1.2 by default, so the handshake fails after the tunnel opens and the proxy gets blamed. Run [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 at the top of the script.

The proxy seems to be ignored

In PowerShell 7, NO_PROXY may match the host, or the process read its default before you changed the variables. In 5.1, environment variables are never read. Compare the address returned by Invoke-RestMethod 'https://api.ipify.org' with and without -Proxy to see which path the request took.

Frequently asked questions

Does Invoke-RestMethod support -ProxyCredential?

Yes. Invoke-RestMethod shares the proxy parameters of Invoke-WebRequest: -Proxy, -ProxyCredential, -ProxyUseDefaultCredentials and, in PowerShell 6 and later, -NoProxy.

Can I put user:pass directly in the -Proxy URI?

Do not rely on it. Windows PowerShell 5.1 drops the credentials, and the documented route in every version is a separate -ProxyCredential. Credentials in the URL are dependable only through HTTP_PROXY in PowerShell 7.

How do I skip the proxy for one command in PowerShell 7?

Add -NoProxy. It ignores the proxy that came from environment variables or system settings for that call only.

Why does PowerShell use my browser's proxy without being told?

With no -Proxy, 5.1 uses the WinINET settings from the Windows proxy page, and PowerShell 7 falls back to them on Windows when no proxy environment variables are set.

Will the proxy IP stay the same for an API that filters by source address?

A ProxyV4 private IPv4 or static ISP proxy keeps its address while it is active, and renewing before expiry keeps the same IP, so a partner-side IP rule does not need updating.

Windows automation with a fixed exit IP

Get a private IPv4 or static ISP proxy that works with -Proxy and -ProxyCredential, keeps its address on renewal and has unmetered traffic.

See live prices

Related articles

Proxy setup
Setting a Proxy on Windows 10 and Windows 11
Set an HTTP(S) proxy on Windows 10 or 11: the Proxy page in Settings, saving the login in Credential Manager, variables for CLI tools and netsh winhttp.
Proxy setup
Using cURL with an authenticated proxy: commands that prove it works
Send cURL requests through an authenticated HTTP(S) proxy with -x and -U, confirm the exit IP, read the CONNECT handshake and time each request on any OS.
Proxy errors
Fixing a 407 from your proxy: a credentials checklist
407 Proxy Authentication Required means the proxy refused your login. The causes in one table, and fixes for Chrome, curl, Python, Selenium and antidetect apps.
Free tools
Check proxies in bulk: status, exit IP and latency
Free online proxy checker: paste up to 100 ip:port:user:pass lines and see which proxies work, their exit IP and latency, plus what each failure result means.
Products
Dedicated datacenter proxies: one private IPv4 per customer
Private IPv4 proxies in the US, Vietnam, Japan, South Korea, Singapore, the UK and Hong Kong. One tenant per IP, HTTP(S) and SOCKS5, no traffic metering.
Proxy setup
Set up your proxy, tool by tool
Proxy setup instructions you can follow with the dashboard open: antidetect browsers, operating systems, browser extensions and scraping libraries, all with authenticated HTTP(S) or SOCKS5.
Knowledge base
Authenticated Proxies in undetected-chromedriver: Extension or Local Forwarder
Set an undetected chromedriver proxy with a login: why --proxy-server drops credentials, an MV3 auth extension, a local forwarder, and how to check the exit IP.
Knowledge base
Configuring Proxies in Crawlee: proxyUrls, Tiers, Custom Functions and Sessions
Crawlee proxy configuration for JS and Python: proxyUrls, tieredProxyUrls, newUrlFunction and session pool binding, with code for a pool of static IPs.
Knowledge base
Sending Postman Requests Through a Proxy, With Login and Exceptions
Postman proxy settings explained: system vs custom proxy, proxy auth, the bypass list, env-variable overrides, agent caveats, and how to prove the exit IP.
Products
Static ISP proxies: residential IPs on consumer networks
Static ISP proxies in the United States and Vietnam: fixed IPs registered to consumer internet providers, rented to one customer, with unmetered traffic.