Skip to content

Instantly share code, notes, and snippets.

@shunia
Last active March 13, 2024 03:00
Show Gist options
  • Save shunia/a123a45763eebc252f065a8081405f1e to your computer and use it in GitHub Desktop.
Save shunia/a123a45763eebc252f065a8081405f1e to your computer and use it in GitHub Desktop.
Windows system-wide proxy settings

The script is intended to work in PowerShell and offers a convinient way to toggle on/off proxy settings across the browser and the powershell environment itself.

How to use

  • Check and get the location of your powershell profile for the current user:
    • Windows Powershell: $HOME\Documents\WindowsPowerShell
    • PowerShell 7: $HOME\Documents\PowerShell
    • If path does not exists, create it
    • refer to this MS doc for more information
  • Enable script excution policy to make the profile work:
    • run command in PowerShell: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    • refer to this MS doc for details about the command arguments and values
  • Copy and paste the provided profile.ps1 file into the profile folder
  • Tweak the content as you want, like change the proxy url, disable unwanted functions, and save it
    • read the content carefully, make sure the values are valid as described
  • Close all of the PowerShell windows and re-open a new PowerShell window to run the command and check the output:
    • to enable proxy run: proxy, and check the output to see if it works
    • to disable proxy run: unproxy, and check the output to see if it works
    • opens a program or browser that should be affected by you proxy settings, and check if it works
# do not change this unless you know what you are doing
$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
# your pac proxy or if you proxy software provides an automatic proxy url, for example: http://127.0.0.1:8080/pac.pac, if none, leave it alone
$automaticproxy = ""
# the manual proxy url, for example: xxx.com:80, if none, leave it alone
$proxy = ""
# urls/paths you don't want to be proxied, comma seperated string, for example: ::1,localhost,127.0.0.1, if none, leave it alone
$noproxy = ""
# set system-wide environment for powershell/cmd proxy
function set_proxy_variable {
# modify system environment for the current user
[System.Environment]::SetEnvironmentVariable("ALL_PROXY", $proxy, "User")
[System.Environment]::SetEnvironmentVariable("HTTP_PROXY", $proxy, "User")
[System.Environment]::SetEnvironmentVariable("HTTPS_PROXY", $proxy, "User")
[System.Environment]::SetEnvironmentVariable("NO_PROXY", $noproxy, "User")
Write-Host "`nCommand line proxy enabled!`n"
}
# unset system-wide environment for powershell/cmd proxy
function unset_proxy_variable {
# modify system environment for the current user
[Environment]::SetEnvironmentVariable("ALL_PROXY", $null, "User")
[Environment]::SetEnvironmentVariable("HTTP_PROXY", $null, "User")
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", $null, "User")
[Environment]::SetEnvironmentVariable("NO_PROXY", $null, "User")
Write-Host "`nCommand line proxy disabled!`n"
}
# set IE proxy, which is much harder and here is not a perfect solution, but it works
function set_IE_proxy {
if ($automaticproxy) {
# set and enable auto proxy
Set-ItemProperty -Path $registryPath -Name AutoConfigURL -Value $automaticproxy
# disable and remove manual proxy
Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 0
Remove-ItemProperty -Path $registryPath -Name ProxyServer
Write-Host "`nIE auto proxy enabled!`n"
}
else if ($proxy) {
Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1
Set-ItemProperty -Path $registryPath -Name ProxyServer -Value $proxy
Set-ItemProperty -Path $registryPath -Name ProxyOverride -Value $noproxy
Write-Host "`nIE manual proxy enabled!`n"
}
else {
Write-Host "`nNo valid IE proxy to be enabled!`n"
}
}
# unset IE proxy
function unset_IE_proxy {
# unset auto proxy
Remove-ItemProperty -Path $registryPath -Name AutoConfigURL
# disable manual proxy
Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 0
Write-Host "`nIE proxy disabled!`n"
}
function enable_proxy {
set_IE_Proxy
set_proxy_variable
Write-Host "`n System-wide proxy enabled!`n"
}
function disable_proxy {
unset_IE_Proxy
unset_proxy_variable
Write-Host "`n System-wide proxy disabled!`n"
}
Set-Alias proxy enable_proxy
Set-Alias unproxy disable_proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment