I know the question is specifically about Powershell 2.0, but I would like to share information on setting up a proxy server in Powershell Core (6+) because it's extremely hard to find elsewhere.
I agree with Dandré, that the best solution is to configure the default proxy server. I just had an issue with Powershell Core (7.1). I was trying exactly what Dandré suggests, but it didn't have any effect. After several hours of research and investigation, I have found out that Powershell Core is probably not using System.Net.WebRequest
to make web requests anymore, but rather System.Net.Nett.HttpClient
.
When I have configured the HttpClient
's default proxy server, all web connections made by Powershell (Invoke-WebRequest
, PowerShellGet's Find-Module
, Install-Module
, etc.) finally started to work.
Configure Default Proxy Server for Powershell Core
If you need to make the configuration permanent, just add the commands below to your Powershell profile.
Configure a specific default proxy server
[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://your-proxy')
If you need to set a specific port, add it to the proxy server URI: http://proxy:1234
.
Configure credentials for authenticating proxy
In case it's an authenticating proxy, you need to set up the credentials to be used for proxy authentication. Use the following command to set 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
If you need different credentials, you can use the Get-Credential
cmdlet, but it's interactive so it's not an ideal solution to have in your Powershell profile. But I'm sure there're other ways.
[System.Net.Http.HttpClient]::DefaultProxy.Credentials = Get-Credential
Configure proxy server bypassing
If you just need to bypass the proxy server and use the direct connection, but Powershell is using the default system-wide proxy server, simply set the HttpClient
's default proxy to null
:
[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy($null)
Add Proxy Configuration to Powershell Profile
To make the configuration permanent, simply add the commands you need to your Powershell profile. There're four of them (All users, all hosts; All users, current host; Current user, all hosts; Current user, current host), but I would use the "Current user, all hosts". To find out the location of a specific profile, use the $Profile
variable with a profile name:
$Profile.CurrentUserAllHosts
It should print the path $Home\Documents\Powershell\profile.ps1
.
If the profile doesn't exist yet, just create it and put the configuration there. It will be executed every time you execute a new Powershell Core (pwsh.exe
) instance.
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.