Using PowerShell Behind a Proxy | Windows OS Hub

/ 2019-06-27 09:41

If you access the Internet in the organization via the proxy server, by default you won’t be able to access an external webpage (Invoke-WebRequest cmdlet), update help using Update-Help cmdlet or download an application package from an external package repository (using PackageManagement or NanoServerPackage) from your PowerShell session. In this article we’ll show how to get the Internet access via proxy server with the authentication from a PowerShell session.

Let’s try to update PowerShell Help:


Or access an external web page:


If you haven’t got a direct Internet connection, the command will return a similar error:

update-help : Failed to update Help for the module(s) ‘DhcpServer, DirectAccessClientComponents….’  with UI culture(s) {en-US} : Unable to connect to Help content. The server on which Help content is stored might not be available. Verify that the server is available, or wait until the server is back online, and then try the command again.

Invoke-WebRequest: Unable to connect to the remote server

Invoke-WebRequest: Unable to connect to the remote server

The matter is that PowerShell (to be more precise, .NET class System.Net.WebClient that is using these cmdlets to address the external resources over HTTP) does not use system proxy settings specified in IE. However, WebClient class has some properties that allow to specify both proxy settings (WebClient.Proxy) and proxy authentication data (WebClient.Credentials or WebClient.UseDefaultCredentials). Let’s consider how to use these properties of WebClient class.

Let’s check the current settings of the system proxy in the PowerShell session:

netsh winhttp show proxy

As you can see, proxy settings are not specified

Current WinHTTP proxy settings:
Direct access (no proxy server).

netsh winhttp show proxy

Import the proxy settings from Internet Explorer parameters:

netsh winhttp import proxy source=ie

or set them manually:

netsh winhttp set proxy ""

netsh winhttp set proxy

If proxy authentication is necessary, the error like “(407) Proxy Authentication Required” will appear when trying to run PoSh commands. Let’s consider two ways of proxy authentication.

If you are signed in using your domain account and your proxy supports NTLM/AD authentication, you can use the credentials of the current user to authenticate on the proxy server (you won’t have to enter your username/password):

$Wcl = new-object System.Net.WebClient
$Wcl.Headers.Add(“user-agent”, “PowerShell Script”)
$Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

If you have to authenticate on the proxy server manually, run the following commands and specify user name and password in the corresponding window.

$Wcl=New-Object System.Net.WebClient

powershell: get credentials to authenticate on a proxy server

Now you can try to access an external website or update the help using Update-Help command.

Using PowerShell from behind authenticated proxy

Everything turned out!

Shared via Inoreader