Skip to content

Instantly share code, notes, and snippets.

@masterflitzer
Created January 3, 2024 17:29
Show Gist options
  • Save masterflitzer/6aef577644911466d3bf04e74d353a05 to your computer and use it in GitHub Desktop.
Save masterflitzer/6aef577644911466d3bf04e74d353a05 to your computer and use it in GitHub Desktop.
Simple script to toggle HTTP proxy on Windows
#Requires -PSEdition Core
[CmdletBinding()]
param (
[Parameter(ParameterSetName = "enable")]
[switch]
$On,
[Parameter(ParameterSetName = "disable")]
[switch]
$Off,
[Parameter(ParameterSetName = "enable", Position = 0, Mandatory = $true)]
[string]
$ProxyAddress,
[Parameter(ParameterSetName = "enable", Position = 1)]
[string]
$ProxyPort = "3128",
[Parameter(ParameterSetName = "enable")]
[bool]
$ProxyBypassInLAN = $true
)
if ($IsWindows -eq $true) {
$ProgressPreferenceOld = $ProgressPreference
$ProgressPreference = "Continue"
if ($On) {
[Environment]::SetEnvironmentVariable("http_proxy", "http://${ProxyAddress}:${ProxyPort}", [EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("https_proxy", "http://${ProxyAddress}:${ProxyPort}", [EnvironmentVariableTarget]::User)
Set-ItemProperty -LiteralPath "HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings" -Name ProxyEnable -Value 1
if ($ProxyBypassInLAN) {
Set-ItemProperty -LiteralPath "HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings" -Name ProxyOverride -Value "<local>"
}
Set-ItemProperty -LiteralPath "HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings" -Name ProxyServer -Value "${ProxyHost}:${ProxyPort}"
}
if ($Off) {
[Environment]::SetEnvironmentVariable("http_proxy", $null, [EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("https_proxy", $null, [EnvironmentVariableTarget]::User)
Set-ItemProperty -LiteralPath "HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings" -Name ProxyEnable -Value 0
Remove-ItemProperty -LiteralPath "HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings" -Name ProxyOverride -ErrorAction SilentlyContinue
Remove-ItemProperty -LiteralPath "HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings" -Name ProxyServer -ErrorAction SilentlyContinue
}
$ProgressPreference = $ProgressPreferenceOld
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment