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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#requires -runasadministrator <# .Synopsis Modify proxy settings for the current user. .DESCRIPTION Modify proxy settings for the current user modifying the windows registry. .EXAMPLE Get the proxy settings for the current user PS D:\> get-proxy ProxyServer ProxyEnable ----------- ----------- 0 .EXAMPLE Set the proxy server for the current user. Test the address and if the TCP Port is open before applying the settings. proxy squid.server.com 3128 # or set-proxy -server "yourproxy.server.com" -port 3128 .EXAMPLE Remove the current proxy settings for the user. .NOTES Author Paolo Frigo, https://www.scriptinglibrary.com #> function Get-Proxy (){ Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' | Select-Object ProxyServer, ProxyEnable } function Set-Proxy { [CmdletBinding()] [Alias('proxy')] [OutputType([string])] Param ( # server address [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] $server, # port number [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] $port ) #Test if the TCP Port on the server is open before applying the settings If ((Test-NetConnection -ComputerName $server -Port $port).TcpTestSucceeded) { Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value "$($server):$($port)" Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 1 Get-Proxy #Show the configuration } Else { Write-Error -Message "The proxy address is not valid: $($server):$($port)" } } function Remove-Proxy (){ Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value "" Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 0 } |
And setting a proxy for the current user can be that simple (taking the advantage of the alias):
1 |
proxy squid.server.com 3128 |
Removing the proxy settings:
1 |
Remove-Proxy |
Get the proxy settings for the current user:
1 |
Get-Proxy |
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.
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.
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
Very good article, thanks
I personally got good results with __link_removed__
I recommend them, they are very good.
Excellent. Solved our problem.
Hi Patrick,
Thank you for your comment and for letting me know.
Regards
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.
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