Get Proxy Settings in PowerShell

PowerShell Get Proxy Settings

1. Introduction

In environments where internet access is controlled through a proxy, PowerShell scripts often need to be aware of these settings to function correctly. This article will guide you through different methods to get proxy settings using PowerShell, focusing on the proxy address, port, and its operational status.

2. Using Net.WebProxy Class

The Net.WebProxy class from the .NET Framework is a straightforward approach to obtain the system’s default proxy settings.

Here,
[System.Net.WebProxy]::GetDefaultProxy(): Retrieves the default proxy settings.
$proxy.Address: Shows the proxy server’s URI.
$proxy.BypassProxyOnLocal: Indicates if local addresses bypass the proxy.

The settings and information obtained through Net.WebProxy are relevant to applications using the .NET framework. They may not reflect proxy configurations used by non-.NET applications or services.

3. Using netsh Command

The netsh (Network Shell) is the command-line tool in Windows, utilized to communicate with different network-related settings.

Let’s use netsh command to get proxy settings:

On Windows machines, the above command is useful for checking the current proxy settings and it is beneficial for verifying proxy settings and troubleshooting network connectivity.

The winhttp specified the context for the netsh, which indicates that we desire to work with the Windows HTTP services settings (WinHTTP settings). The show proxy is the particular command within the WinHTTP context, which displays the current proxy settings on the PowerShell console.

After executing this command, we got the current proxy server, port number, and any bypass list that had been configured (see the above output).

It’s important to note that netsh winhttp show proxy does not display the proxy settings configured for a user in their web browser (like Internet Explorer, Chrome, or Firefox). Those settings are user-specific and are stored separately from the WinHTTP settings.

4. Accessing Internet Explorer Proxy Settings

Accessing Internet Explorer Proxy Settings involves querying the Windows Registry to find out the proxy configuration specifically set for Internet Explorer (IE). This approach is based on the fact that many Windows applications, including some older or legacy systems, use the proxy settings defined in Internet Explorer.

The above code got the proxy settings from the Windows registry. How? In this example, we used the Get-ItemProperty cmdlet to get the properties of the specified item. In our case, it was the registry key specified using the -Path parameter.

In the registry key path, the HKCU denotes the HKEY_CURRENT_USER hive in the Windows registry. This registry key contains Internet Settings, particularly the proxy settings for the current user. We piped the output of the Get-ItemProperty cmdlet to the Select cmdlet to choose the ProxyEnable and ProxyServer properties with corresponding values.

The ProxyEnable shows the status of the proxy whether it is enabled or not. If the value of the ProxyEnable property is 1, it meant the proxy is enabled. Whereas, the value of 0 for this property represents that the proxy is not enabled. The ProxyServer property contains the proxy-server related details as Server_IP:Port.

The Select cmdlet is an alias of the Select-Object cmdlet in PowerShell.

5. Using WebRequest Class

This method involves creating a web request to extract proxy settings using WebRequest class.

WebRequest: creates an object for a specific URL.
$request.Proxy: Retrieves the proxy used by this request.
GetProxy($request.RequestUri).ToString(): Outputs the proxy’s URI.

6. Using Windows Settings

Navigate to the Windows Settings > Network & Internet > Proxy and see the details about the proxy server; check the following screenshot.

powershell get proxy settings - proxy server details

We can also use chrome://net-internals/#proxy in Google address bar and click Enter.

7. Conclusion

We explored four methods to obtain proxy settings in PowerShell, catering to various network configurations and requirements.

These methods range from using .NET Framework classes, querying the Windows Registry, to executing system commands like netsh. Each technique offers a reliable way to ensure that PowerShell scripts can correctly identify and utilize proxy settings in a networked environment.

Was this post helpful?

Leave a Reply

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