By default, if you can only access the Internet from your computer through a proxy server, you cannot access external Web resources through a PowerShell session: the Web page (Invoke-WebRequest command), update the help with the Update-Help command, connect to Office365/Azure, or download an application package from a remote package repository (using PackageManagement or NanoServerPackage). In this article, I will show you how to access the Internet from a PowerShell session through an authenticated proxy server.

Let’s try updating PowerShell Help from a computer behind the proxy:

Updating help

Or call an external website:

If you do not have a direct connection to the Internet, the command returns a similar error:

Update – Help : The help for the DhcpServer module(s), DirectAccessClientComponents…. with {in-US} user interface culture(s) could not be updated : No links can be made with the content of the aid. The server on which the reference material is stored may not be available. Check if the server is available or wait until the server is back online and try the command again.
Invec-WebRequest : Cannot connect to the remote server.

Nullity: (System.Net.HttpWebRequest:HttpWebRequest).

The fact is that PowerShell (specifically the .NET System.Net.WebClient class that these commands use to access external resources via HTTP/HTTPS) does not use the proxy settings set by Internet Explorer. However, the WebClient class has certain properties that allow it to specify both proxy settings (WebClient.Proxy) and proxy authentication credentials (WebClient.Credentials or WebClient.UseDefaultCredentials).

Manage WinHTTP proxy settings for PowerShell

Let’s check the current system proxy settings via PowerShell :

show the winhttp proxy network

As you can see, the proxy settings are not specified:

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

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

netsh winhttp import proxy source=ie

or set it up manually:

netsh winhttp set proxy

If proxy authentication is required, an error such as (407) Proxy authentication required is displayed when you try to execute PowerShell commands. If you z. B. try logging into your Azure subscription with the command :

Add-AzureAccount – Credit (Get-Credential)

There’s been a mistake:

Add-AzureAccount : user_realm_discovery_failed : Detection of the user space has failed: The remote server returned an error: (407) Proxy authentication is required.

How do I configure proxy authentication with PowerShell?

Let’s look at two ways to use proxy authentication: You can use Active Directory SSO authentication, or you can specify user credentials for manual authentication.

If you are logged in to your computer under a domain account and your proxy server supports Active Directory Kerberos or NTLM authentication (if you have not already disabled it), you can use your current user credentials to authenticate to the proxy server (you do not need to enter your user name and password):

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

If you need to manually authenticate to the proxy server, run the following commands and enter your username and password in the appropriate authentication window.

$Wcl=New Object System.Net.WebClient

You can now try to access the remote site or update the help with the Update Help command.

As you can see, the Invoke-Web Request command returns the data from the site’s external web page!

Setting proxy settings in the PowerShellprofile file

You can create a PowerShell profile file to automatically set the proxy settings when PowerShell is launched.

To do this, run a command that creates a PowerShell profile file (C:UsersusernameDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1) :

Notepad $PROFILE (or Notepad $PROFILE.AllUsersCurrentHost – if you need to apply a PowerShell profile to all users on the computer). A PowerShell profile is a PS script that is executed when you run the PowerShell.exe process.

Copy the PowerShell code to a notebook window. You use z. B. PAC (Proxy Auto Configuration) files to automatically configure proxy settings on users’ computers. Use the following PowerShell profile script to specify the URL of the PAC file and authenticate as the current user on the proxy server.]::DefaultWebProxy = new-object‘’)
# If you need to import the proxy settings from Internet Explorer, you can replace the previous line with: netsh winhttp import proxy source=ie
[]::DefaultWebProxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
# You can retrieve user credentials :
# System.Net.WebRequest]::DefaultWebProxy.Credentials = Get-Credential
# You can also retrieve the user’s password from a saved XML file (see Using Saved Credentials in PowerShell Scripts) :
# System.Net.WebRequest]::DefaultWebProxy= Import-Clixml – Path C:PSuser_creds.xml
[]::DefaultWebProxy.BypassProxyOnLocal= $true

By default, the PowerShell Script Execution Policy does not allow all PS scripts to be executed, even from the PowerShell profile files. To run scripts, you must change PowerShell’s execution policy. Execute the command:

Policy for remote implementation

Save the Microsoft.PowerShell_profile.ps1 file and restart the PowerShell console window. Now make sure you can access web resources from a PowerShell session through the proxy server without having to run any additional commands.

Check the current proxy settings via PowerShell

You can get the current proxy settings from the registry using the PowerShell command:

GetProperty – path ‘HKCU:SoftwareMicrosoftWindowsCurrentVersionInterneteinstellungen’ | Select-Object ProxyServer, ProxyEnable

In my example, this is the address and port of the proxy server:
Proxy server enabled : ProxyEnable =1

You can also get these WebProxy settings:


If necessary, you can activate the use of a proxy server with the following command:

Set-ItemProperty – path ‘HKCU:SoftwareMicrosoftWindowsCurrentVersionInterneteinstellungen’ ProxyEnable – value 1

To deactivate the proxy :
Set-ItemProperty – path ‘HKCU:SoftwareMicrosoftWindowsCurrentVersionInterneteinstellungen’ ProxyEnable – value 0

Set up a Windows proxy with PowerShell?

You can use PowerShell to define the proxy settings for the current Windows user. For example, you can change the proxy settings with the following PowerShell function, but first you test the availability of the proxy server and the port response to it using the Test-NetConnection cmdlet

Set-Proxy( $server,$port) function
If ((Test-NetConnection -ComputerName $server -Port $port).TcpTestSucceded) {
Set-ItemProperty -Path ‘HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings’ -name ProxyServer -Value $($server):$($port)
Set-ItemProperty -Path ‘HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings’ -name ProxyEnable -Value 1
Total {
spelling error – message Invalid proxy address or port: $($server):$($port)

Establishment of the power of attorney 3128

Related Tags:

powershell get proxy settingsset proxy for powershell sessionpowershell bypass proxypowershell check proxy settings remotelypowershell invoke-webrequest proxy credentialspowershell set proxy for all userspowershell iwr proxy authenticationpowershell set proxy environment variable

By ryui

Leave a Reply

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