If you are an IT administrator, this tutorial will be very useful. In this tutorial, I will explain how to set up a proxy in PowerShell. Proxy is helpful for network management, security, and accessing web resources from behind a firewall. Whether you’re an IT professional working in a corporate environment or a developer needing to route your web requests through a proxy, this guide will provide detailed steps.
Set Proxy in PowerShell
In a corporate setting, proxies are often used to enforce internet usage policies and monitor employee activity.
PowerShell offers multiple ways to set up a proxy. In this guide, we will cover:
- Setting proxy using
System.Net.WebRequest - Configuring proxy settings via environment variables
- Using
Invoke-WebRequestwith a proxy
Check out Get Windows Event Logs using PowerShell
1. Setting Proxy Using System.Net.WebRequest
The System.Net.WebRequest class provides a way to configure proxy settings directly in your PowerShell scripts. This method is useful for making HTTP requests through a proxy server.
Example:
Imagine you are working for a tech company, and you need to fetch data from an external API that requires routing through a proxy server.
# Define the proxy server
$proxy = New-Object System.Net.WebProxy("http://proxyserver:8080", $true)
# Set the proxy credentials
$proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
# Create a WebRequest object
$request = [System.Net.WebRequest]::Create("http://api.example.com/data")
$request.Proxy = $proxy
# Get the response
$response = $request.GetResponse()
$reader = [System.IO.StreamReader] $response.GetResponseStream()
$content = $reader.ReadToEnd()
# Display the content
Write-Output $contentIn this example, we create a WebProxy object, set the proxy credentials, and assign the proxy to the WebRequest object. This allows the request to be routed through the specified proxy server.
Check out Uninstall Firefox Using PowerShell
2. Configure Proxy Settings via Environment Variables
Another way to configure proxy settings in PowerShell is by setting environment variables. This method is straightforward and affects all web requests made by PowerShell.
Example:
Let’s say you’re an IT administrator in New York City, and you need to configure proxy settings for all users on a network. You can set the environment variables using the following script:
# Set the HTTP proxy
[System.Environment]::SetEnvironmentVariable("HTTP_PROXY", "http://proxyserver:8080", [System.EnvironmentVariableTarget]::User)
# Set the HTTPS proxy
[System.Environment]::SetEnvironmentVariable("HTTPS_PROXY", "http://proxyserver:8080", [System.EnvironmentVariableTarget]::User)
# Verify the settings
Write-Output "HTTP_PROXY: $([System.Environment]::GetEnvironmentVariable('HTTP_PROXY', [System.EnvironmentVariableTarget]::User))"
Write-Output "HTTPS_PROXY: $([System.Environment]::GetEnvironmentVariable('HTTPS_PROXY', [System.EnvironmentVariableTarget]::User))"This script sets the HTTP_PROXY and HTTPS_PROXY environment variables for the current user. You can also set these variables system-wide by changing [System.EnvironmentVariableTarget]::User to [System.EnvironmentVariableTarget]::Machine.

Read Get and Set Window Size in PowerShell
3. Using Invoke-WebRequest with a Proxy
The Invoke-WebRequest cmdlet is a powerful tool for making web requests in PowerShell. You can easily configure it to use a proxy server.
Example:
Suppose you’re a data analyst in Chicago, and you need to download data from a remote server through a proxy. Here’s how you can do it:
# Define the proxy server
$proxy = New-Object System.Net.WebProxy("http://proxyserver:8080", $true)
# Set the proxy credentials
$proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
# Make the web request
$response = Invoke-WebRequest -Uri "http://api.example.com/data" -Proxy $proxy
# Display the content
Write-Output $response.ContentIn this example, we create a WebProxy object and pass it to the Invoke-WebRequest cmdlet using the -Proxy parameter. This ensures that the request is routed through the specified proxy server.
Read Get Window Titles Using PowerShell
Handle Proxy Authentication
In some cases, the proxy server may require authentication. You can handle this by explicitly setting the proxy credentials.
Example:
Imagine you are a network engineer in Los Angeles, and you need to authenticate with a proxy server to access an external resource.
# Define the proxy server
$proxy = New-Object System.Net.WebProxy("http://proxyserver:8080", $true)
# Set the proxy credentials
$proxy.Credentials = New-Object System.Net.NetworkCredential("username", "password")
# Make the web request
$response = Invoke-WebRequest -Uri "http://api.example.com/data" -Proxy $proxy
# Display the content
Write-Output $response.ContentIn this script, we create a NetworkCredential object with the username and password required for authentication and assign it to the proxy.
Check out Get Windows Services Using PowerShell
Troubleshooting Common Issues
Let me show you some common issues and tell you how to troubleshoot and fix them while working with proxy settings using PowerShell.
Proxy Server Not Responding
If the proxy server is not responding, ensure that the server address and port are correct. You can test the connection using a web browser or a different tool to verify that the proxy server is accessible.
Authentication Errors
If you encounter authentication errors, double-check the credentials and ensure that they have the necessary permissions to access the proxy server. You may also need to verify that the proxy server supports the authentication method you are using.
Network Configuration
Ensure that your network settings allow traffic through the proxy server. This may involve configuring firewall rules or adjusting network policies.
Conclusion
Setting up a proxy in PowerShell is useful for managing network traffic, enhancing security, and accessing web resources from behind a firewall. By following the methods and examples provided in this guide, you can configure proxy settings in PowerShell efficiently and effectively.
Do let me know in the comment below if these steps help you.
You may also like:
- Get Windows Activation Status Using PowerShell
- List Drives in PowerShell
- Get Computer Information Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.