Bypass proxy for all web requests in Powershell Core

Solution 1:

Finally, after several hours of researching and messing around, I was able to solve my issue thanks to this GitHub issue comment!

I don't know the background, but it looks like Powershell Core is actually using System.Net.Http.HttpClient rather than System.Net.WebRequest for making web requests.

When I have learned that, it was quite easy to configure HttpClient's default proxy.

1. Manually Configure HttpClient.DefaultProxy

Proxy Bypassing

To bypass the proxy (use the direct connection), set it to null:

[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy($null)

I have added it to my Powershell profile and all connections made by Powershell started to work.

Configure Specific Proxy Server

To configure an actuall proxy server instead, use the following command:

[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://proxy', $true)

Configure Proxy Authentication

In case it's an authenticating proxy, you have to configure credentials to be used for the proxy authentication. The following command will use the credentials of your domain account under which you're currently logged in to Windows:

[System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

2. Configure Default Proxy using Environment Variable

An alternative solution is to use an environment variable. According to the documentation, HttpClient is actually using the following variables to initialize the default proxy, if they're defined:

  • HTTP_PROXY for HTTP requests
  • HTTPS_PROXY for HTTPS requests
  • ALL_PROXY for both HTTP and HTTPS
  • NO_PROXY may contain a comma-separated list of hostnames excluded from proxying

An example usage:

ALL_PROXY='http://proxy:1234'

And if you need to pass credentials:

ALL_PROXY='http://username:password@proxy:1234'

This solution is supported by a wider range of applications, so if it's better or worse than the previous solution depends on what exactly you want to configure, just Powershell or also other applications.