Windows: The comprehensive guide on how to set system level proxy settings - including authentication - from the command line in modern versions of Windows
There are many cases where you might want to set the proxy of a Windows machine at the system level, so that all apps go through this proxy. If you've looked into this before, you've probably found that it's really easy - in the UI at least - simply go to Settings -> Network & internet -> Proxy and you can set it there. What isn't so obvious, at least in modern versions of Windows, is how to set the proxy via the command line - there are plenty of situations where you might want to do this to automate proxy set up. The main problem is that, they way in which the system level proxy and authentication is set at the system level, has changed in modern versions of Windows - so lots of information about it on the internet, is out of date.
👥 Setting the proxy at the user level
Setting the proxy server
So, first thing's first, we need to set the proxy server address. In Windows, most of the proxy settings are stored at the following address in the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings.
Here, you can set the value ProxyServer to the address of the proxy server you want to use, along with the port. The, you need to set the ProxyEnable value to 1 to actually enable the proxy. There are other values you can change here as well. You can play around in Settings -> Network & internet -> Proxy and see how these values change accordingly if you need.
We can set these values using the reg command line utility like so:
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxyServer" /d "[PROXY]" /f
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxyEnable" /t REG_DWORD /d 1 /f
Ensure to replace [PROXY] with your proxy server and port. Note that the /f is important - this forcefully overwrites the value if it already exists - if it doesn't do this, the command can hang waiting for user input.
Setting the authentication
If your proxy server needs authentication to access, you'll need to set that as well. This can be the tricky part, as this information is not set in the same place as the proxy server. It is instead, set as a generic credential in the Windows credential manager. Normally, when enabling the proxy through the UI, Windows will prompt you to enter the credentials for the server, and then automatically save them in the Windows Credential Manager. However, we can add them via the command line instead if we want.
cmdkey /generic:[DOMAIN] /user:[USERNAME] /password:[PASSWORD]
Of course, be sure to replace everything within the square brackets with the appropriate values. Also, it's important to note, that, this can only be done in an interactive terminal session, as cmdkey does not work in non-interactive sessions.
🖥️ Can the proxy be set at the machine level (instead of user level)?
Proxy
It is possible to set the proxy at the computer level instead of user level by firstly, setting the same values as above, but in HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER (so HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings instead). However, in order for these settings to be used, you must set ProxySettingsPerUser located at HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings to 0.
reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxySettingsPerUser" /t REG_DWORD /d 0 /f
Authentication
Authentication is a little more tricky - since credentials in the Windows Credential Manager are per-user not per-machine. However, one way you could work around this could be by using a script that runs on user login that sets the credentials. For example, you could use the windows Task Scheduler to do this, or put the script in the user's startup folder (%appdata%\Microsoft\Windows\Start Menu\Programs\Startup).
This snippet is available in Codly. Click the download link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store.
Comments
Post a Comment