How to Test a Proxy in PowerShell
PowerShell can pick up proxy settings from the environment or system, or use a proxy you supply for one request. Compare both routes from the same shell and see which IP the destination reports.
Check the PowerShell version and choose a test route
Open pwsh and check $PSVersionTable.PSVersion before testing. The script below uses the timeout parameter names available in PowerShell 7.4 and later. Windows PowerShell 5.1, often started as powershell.exe, uses a different HTTP stack and parameter set. If you are diagnosing a scheduled task or CI job, check which PowerShell executable it actually runs and test under that account. The same machine can have different proxy settings for different users or services. Invoke-WebRequest parameter reference.
Use a single HTTPS test URL and compare three routes. The default route shows the behavior of PowerShell under its current configuration. The explicit route uses the proxy you specify. When your network permits a direct connection to the test URL, a third run gives you a baseline with PowerShell’s proxy selection disabled. Keep the approved routing in place when a managed network requires a proxy. The commands make one GET request each and read the ip field from the same endpoint.
Run a PowerShell proxy check and record the exit IP
Save this script as Test-ProxyRoute.ps1. Use the proxy host and port supplied by its operator in the explicit test. This example uses a plain HTTP connection to the proxy and an HTTPS destination. For this pairing the proxy opens an HTTP CONNECT tunnel before PowerShell establishes TLS with the destination through it. The script returns a row with the route, HTTP status and observed IP. It keeps the endpoint fixed and certificate verification enabled. How HTTP and HTTPS proxies handle HTTPS requests.
param(
[ValidateSet('Default', 'Explicit', 'Direct')]
[string] $Mode = 'Default',
[uri] $ProxyUri,
[switch] $PromptForProxyCredential
)
$request = @{
Uri = 'https://testmyproxies.com/api/my-ip'
ConnectionTimeoutSeconds = 10
OperationTimeoutSeconds = 20
MaximumRedirection = 0
ErrorAction = 'Stop'
}
if ($PromptForProxyCredential -and $Mode -ne 'Explicit') {
throw 'Proxy credentials are only used in Explicit mode.'
}
switch ($Mode) {
'Explicit' {
if (-not $ProxyUri -or -not $ProxyUri.IsAbsoluteUri -or
$ProxyUri.Scheme -ne 'http' -or -not $ProxyUri.Host -or
$ProxyUri.UserInfo -or $ProxyUri.AbsolutePath -ne '/' -or
$ProxyUri.Query -or $ProxyUri.Fragment) {
throw 'Use an HTTP proxy URI such as http://proxy.example:3128.'
}
$request.Proxy = $ProxyUri
if ($PromptForProxyCredential) {
$request.ProxyCredential = Get-Credential -Message 'Enter the proxy account'
if (-not $request.ProxyCredential) {
throw 'No proxy credential was supplied.'
}
}
}
'Direct' {
$request.NoProxy = $true
}
}
$response = Invoke-WebRequest @request
if ([int] $response.StatusCode -ne 200) {
throw "The IP endpoint returned HTTP $($response.StatusCode)."
}
$payload = $response.Content | ConvertFrom-Json -ErrorAction Stop
$address = $null
if ($payload.ip -isnot [string] -or
-not [ipaddress]::TryParse($payload.ip, [ref] $address)) {
throw 'The response did not contain a valid IP address.'
}
[pscustomobject] @{
Route = $Mode
Status = [int] $response.StatusCode
ExitIP = $address.ToString()
}
Run the first two commands in the same shell and compare their output. Run the third only when direct access to that endpoint is allowed. Add -PromptForProxyCredential to the explicit command if the proxy expects a username and password. PowerShell prompts interactively and supplies the resulting credential object to -ProxyCredential.
.\Test-ProxyRoute.ps1
.\Test-ProxyRoute.ps1 -Mode Explicit -ProxyUri http://proxy.example:3128
.\Test-ProxyRoute.ps1 -Mode Direct
Compare the exit IP from the explicit run with the exit or address range the provider expects. The proxy’s entry hostname can resolve to a different address from the one TestMyProxies sees. The two routes can also share the same outward address. If the IPs match, verify the chosen route against the proxy’s connection logs when available. Compare browser and proxy exit IPs.
Find where PowerShell gets the default proxy
PowerShell 7 uses .NET’s HttpClient.DefaultProxy for requests without an explicit proxy setting. On Windows and macOS, the runtime looks for proxy settings in environment variables and can fall back to platform settings. On Linux the default is a direct client when no proxy configuration is supplied through the environment. Look at HTTPS_PROXY, ALL_PROXY and NO_PROXY in the process running the command. An exclusion that matches the IP lookup can explain why the default mode returns the address you expected from a direct request. .NET default proxy rules · Understand proxy environment variables.
A runner can inherit values from the service that launched it. An interactive terminal you opened later can have a different environment, even under the same account. If results vary, run the check from the same job or service context as the failing command. When testing changed environment settings, start a new PowerShell process with those settings present before the first request. Avoid printing the full values into CI logs because proxy URLs supplied by a deployment can include credentials.
To inspect the default proxy’s decision without issuing the request, use its IsBypassed method. Check the same destination in the same process, since a bypass decision is specific to the target. True means this default proxy object treats the URL as a direct route. False means its configuration selects a proxy for the URL. The method reports the routing decision, while the actual request verifies connectivity, authentication and the observed IP. The explicit test sets its own proxy for that request and may choose a different route. .NET proxy bypass reference.
$target = [uri]'https://testmyproxies.com/api/my-ip'
[System.Net.Http.HttpClient]::DefaultProxy.IsBypassed($target)
Troubleshoot 407, timeouts and certificate errors
A 407 or a failed tunnel. Check the proxy’s authentication method and account. Use -ProxyCredential with -Proxy for credentials going to the proxy. The cmdlet’s -Credential parameter serves HTTP authentication to the destination. A rejected CONNECT may appear in an exception instead of a normal response object. If the error message lacks a status, run a cURL check from the same machine and read its separate CONNECT result. Understand HTTP 407 · Inspect the connection with cURL.
Connection refused or request timed out. Confirm the proxy’s hostname and port, whether the machine can resolve the hostname and whether that process can reach the proxy network. The script sets separate limits for a pending request and for reads from the response stream. DNS can take longer than the configured connection timeout in some cases. The operation timeout limits time spent waiting on a read. Compare where the failure occurred before increasing either value. PowerShell operation timeout.
A certificate error. Verify the hostname and certificate chain used by the failing machine. If your organization manages inspection of HTTPS traffic, check whether the approved CA is installed in the trust store available to the job’s account. Keep validation enabled so a successful check also confirms the peer’s identity. Diagnose proxy TLS errors.