Skip to content

Instantly share code, notes, and snippets.

@wmeints
Last active May 16, 2024 10:12
Show Gist options
  • Save wmeints/59659f540ecc7c4b6340dadbb08bd4df to your computer and use it in GitHub Desktop.
Save wmeints/59659f540ecc7c4b6340dadbb08bd4df to your computer and use it in GitHub Desktop.
This script allows you to enable and disable your proxy settings quickly in case you work from two different locations like your home office and a workplace where they require you to use a proxy.
$internetSettings = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
# Determines the current state of the proxy
function Get-ProxyState() {
$settings = Get-ItemProperty -Path $internetSettings;
return $settings.ProxyEnable;
}
# Enables the proxy.
# IMPORTANT: Change the defaults to something that is useful to you!
function Enable-Proxy(
[String] $ProxyServer = "",
[String] $ProxyScript = "",
[String] $ProxyOverrides = "") {
$proxyState = Get-ProxyState
if($proxyState -eq 0) {
Write-Host "Proxy was not enabled, enabling it."
Set-ItemProperty -Path $internetSettings -Name ProxyServer -Value $ProxyServer
Set-ItemProperty -Path $internetSettings -Name AutoConfigURL -Value $ProxyScript
Set-ItemProperty -Path $internetSettings -Name ProxyOverride -Value $ProxyOverrides
Set-ItemProperty -Path $internetSettings -Name ProxyEnable -Value 1
# Also update the environment variables to the right setting.
# This enables tools like GIT and NPM to use the proxy.
$env:HTTP_PROXY = "$proxyServer"
$env:HTTPS_PROXY = "$proxyServer"
}
}
# Disables the proxy everywhere
function Disable-Proxy() {
$proxyState = Get-ProxyState
if($proxyState -eq 1) {
Write-Host "Proxy was enabled, disabling it."
# Disable the proxy and remove the auto configure script.
# Otherwise it is partially enabled.
Set-ItemProperty -Path $internetSettings -Name ProxyEnable -Value 0
Set-ItemProperty -Path $internetSettings -Name AutoConfigURL -Value ""
Set-ItemProperty -Path $internetSettings -Name ProxyOverride -Value ""
# Remove the proxy environment variables so that tools like GIT and NPM
# won't complain about the proxy being unreachable.
$env:HTTP_PROXY = $Null
$env:HTTPS_PROXY = $Null
}
}
# Toggles the proxy in Windows
function Toggle-Proxy() {
$proxyState = Get-ProxyState
if($proxyState -eq 0) {
Enable-Proxy
}
else {
Disable-Proxy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment