Using PowerShell Behind a Proxy Server

If you possibly can entry the Internet out of your laptop solely through a proxy server, then by default you received’t be capable to entry exterior internet assets out of your PowerShell session: a webpage ( cmdlet), replace assist utilizing the Update-Help cmdlet, connect with Office365/Azure, or obtain an software package deal from an exterior package deal repository (utilizing or ). In this text we’ll present you the best way to entry internet from a PowerShell session through a proxy server with the authentication.

Let’s attempt to replace the PowerShell Help frow a laptop behind a proxy server:


Or entry an exterior internet web page:


If you haven’t received a direct Internet connection, the command will return a related error:
Update-help : Failed to replace Help for the module(s) ‘DhcpServer, DirectAccessClientComponents….’  with UI tradition(s)  : Unable to hook up with Help content material. The server on which Help content material is saved won't be out there. Verify that the server is accessible, or wait till the server is again on-line, after which strive the command once more.
Invoke-WebRequest: Unable to hook up with the distant server.

InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest).

The matter is that PowerShell (or slightly, the .NET class System.Net.WebConsumer, which these cmdlets used to entry exterior assets over HTTP/HTTPS) doesn’t use proxy settings . However, the WebConsumer class has some properties that help you specify each proxy settings (WebConsumer.Proxy) and proxy authentication knowledge (WebConsumer.Credentials or WebConsumer.UseDefaultCredentials). Let’s contemplate the best way to use these properties of the WebConsumer class.

Manage WinHTTP Proxy Server Settings for PowerShell

Let’s test the present settings of the system proxy from  PowerShell:

netsh winhttp present proxy

As you possibly can see, proxy settings will not be specified.

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

You can import proxy server settings from the Internet Explorer parameters:

netsh winhttp import proxy supply=ie

or set them manually:

netsh winhttp set proxy ""

If proxy authentication is critical, the error like “(407) Proxy Authentication Required” will seem while you making an attempt to run PowerShell instructions. For instance, while you strive to hook up with your Azure subscription with the command:

Add-AzureAccount -Credential (Get-Credential)

An error occurred:

Add-AzureAccount : user_realm_discovery_failed: User realm discovery failed: The distant server returned an error: (407) Proxy Authentication Required.

How to Set Proxy Authentication with PowerShell

Let’s contemplate two methods of utilizing proxy authentication: you should utilize Active Directory SSO authentication, or specify consumer credentials for authentication manually.

If you’re licensed in your laptop beneath a area account, and your proxy server helps Active Directory Kerberos, or NTLM authentication (in case you have not it but), then you should utilize the present consumer credentials to authenticate on the proxy server (you do not want to enter your username and password):

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

If it’s good to authenticate on the proxy server manually, run the next instructions and specify consumer title and password within the corresponding credential window.

$Wcl=New-Object System.Net.WebConsumer

Now you possibly can attempt to entry an exterior web site or replace the assistance utilizing Update-Help command.

As you possibly can see, the Invoke-Web Request cmdlet returned knowledge from the exterior website webpage!

Set Proxy Server Settings within the PowerShell Profile File

You can create a PowerShell profile file to mechanically set proxy settings settings when PowerShell begins.

To do that, run the command that can create the PowerShell profile file (C:UsersusernameDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1):

notepad $PROFILE (or notepad $PROFILE.AllUsersCurrentHost – if it’s good to apply a PowerShell profile to all customers of the pc).

A PowerShell profile is a PS script that runs when your PowerShell.exe course of begins.

Copy your PowerShell code into the notepad home windows. For instance, you’re utilizing the Proxy Auto-Configuration (PAC) recordsdata to mechanically configure proxy server settings on consumer computer systems. You can specify the URL handle of the PAC file and the authenticate on the proxy server beneath the present consumer with the next PowerShell profile script.

[]::DefaultWebProxy = new-object system.internet.webproxy('')
# If it's good to import proxy settings from Internet Explorer, you possibly can substitute the earlier line with the: "netsh winhttp import proxy supply=ie"
[]::DefaultWebProxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
# You can request consumer credentials:
# System.Net.WebRequest]::DefaultWebProxy.Credentials = Get-Credential
# Also, you may get the consumer password from a saved XML file (see the article “”):
# System.Net.WebRequest]::DefaultWebProxy= Import-Clixml -Path C:PSuser_creds.xml
[]::DefaultWebProxy.BycrossProxyOnLocal = $true

By default, the PowerShell script Execution Policy doesn’t enable all PS scripts to run, even from a PowerShell profile recordsdata. To enable scripts to run, it’s good to change your PowerShell Execution Policy. Run the command:

Set-ExecutionPolicy RemoteSigned

Save the Microsoft.PowerShell_profile.ps1 file and restart the PowerShell console window. Make certain which you could now entry Web assets from a PowerShell session through  a proxy with out the necessity to run extra instructions.

Check Current Proxy Server Setting from PowerShell

You can get the present proxy settings command:

Get-ItemProperty -Path 'HKCU:Software programMicrosoftWindowsCurrentModelInternet Settings' | Select-Object ProxyServer, ProxyAllow

In my instance, the handle and port of the proxy server are:
Proxy server enabled: ProxyAllow =1

You may also get WebProxy settings like this:


If essential, you possibly can allow using proxy with the next command:

Set-ItemProperty -Path 'HKCU:Software programMicrosoftWindowsCurrentModelInternet Settings' ProxyAllow -value 1

To disable proxy:
Set-ItemProperty -Path 'HKCU:Software programMicrosoftWindowsCurrentModelInternet Settings' ProxyAllow -value zero

Set Windows Proxy Setting Using PowerShell?

You can set proxy settings for present Windows consumer utilizing PowerShell. For instance, the next PowerShell operate permits you to change proxy settings, however first it checks the provision of the proxy server and the port response on it utilizing the

operate Set-Proxy ( $server,$port)

Set-Proxy 3128

Check Also

Configuring Network Adapter Settings with PowerShell: IP Address, DNS, Default G…

In this text we’ll present you find out how to configure community adapter parameters in …

Leave a Reply

Your email address will not be published. Required fields are marked *