How To Modify Your Proxy Settings with Powershell

Proxy servers are everywhere and used for more than one purpose. Frequently a bad configuration of a proxy server on the device can be the root cause of users navigation problems or negative experience, needless to say, that companies use group policies to manage proxy settings or transparent proxy in a router/firewall to avoid this problem.

But in general, a proxy server has a positive impact on the user experience and sometimes the user is not even aware of it.

From the user point-of-view setting a proxy server can be a manual and boring process to set up, in an MS Windows OS: open control panel, internet options, connection, lan settings, properties, enable the proxy and providing address and port of the proxy.

If you want to test different proxy and set them up frequently that manual process can be done via this simple Powershell script or even automated.

The feature offered by this script is that the connection to the proxy server and the TCP port is tested before applying any change to the windows registry for the current user internet option.

And setting a proxy for the current user can be that simple (taking the advantage of the alias):

Removing the proxy settings:

Get the proxy settings for the current user:

Conclusions

Every user can modify his proxy settings with the GUI or via this PowerShell function defined in my script but doesn’t mean that a transparent proxy is already present at the network level and it will not affect by any type of changes to the end-user device configuration.

7 Replies to “How To Modify Your Proxy Settings with Powershell”

  1. Thanks for the great info.
    Would you happen to know a way to get the IP from the PC and then set the internet proxy using the 2nd octet from the IP address?
    So, I have multiple schools and each has a proxy server, so I want to be able to set the proxy based on where that PC is.
    Location 1 gives PC IP address of 100.55.50.100, Proxy at this location is 100.55.100.1:9090
    Location 2 gives PC IP address of 100.60.50.26, Proxy at this location is 100.60.100.1:9090
    The second octet is what changes between locations for both proxy address and IP schema.
    I am trying to do a script at login for the user, so that if the device moves locations, it will not need to have the proxy manually re-entered to get back on the internet.

  2. Hi Manny,
    Thanks for your comment. This is an interesting question.
    This code would answer your question

    $MyPrivateIP = "100.55.50.100"
    $SecondOctet = $MyPrivateIP.Split(".")[1]
    $MyProxyIP = "100.$SecondOctet.100.1:9090"

    Write-Output "if this is my private ip $MyPrivateIp this is my second octet $SecondOctet and consequently, this is my proxy address $MyProxyIP"

    This would be the output

    if this is my private ip 100.55.50.100 this is my second octet 55 and consequently, this is my proxy address 100.55.100.1:9090

    The problem with this approach is the assumptions we made, for instance, that we get the right private IP or we expect a single network card or the user is connected to the right network.

    So you might need to get the ip filtering in a similar manner:

    Get-NetIPAddress -AddressFamily IPV4 | select-object -expandproperty IPAddress | Where-Object {$_ -like "100*"}
    100.55.50.100

    So we need to get a sort of proper validation or filter.

    I’m sure that you have already considered implementing a (site-based) GPO but you have decided to go down the scripting route instead.

    I hope this answered all your questions.

    Thanks,
    Regards

  3. Very good article, thanks
    I personally got good results with __link_removed__
    I recommend them, they are very good.

  4. Hi, thanks for the info.
    Please tell me how you can set the proxy settings not for the user, but for the entire system at once. If the computer is not in the domain.

    1. Hi Maxim,
      Thanks for your comment. Your question is interesting. Yes, this script applies the proxy setting at the user level. You would try replacing HKLM instead of HKCU.
      An alternative way could be run this old school netsh command at the end of the powershell script netsh winhttp import proxy ie.
      I haven’t tried these methods above, but it should work.
      Regards

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.