Skip to content

Instantly share code, notes, and snippets.

@AliMilani
Last active February 13, 2023 10:32
Show Gist options
  • Select an option

  • Save AliMilani/cf43983869441d4e1eb8c2c588ff542f to your computer and use it in GitHub Desktop.

Select an option

Save AliMilani/cf43983869441d4e1eb8c2c588ff542f to your computer and use it in GitHub Desktop.
set windows and cmd proxy with powershell (default gateway)
# https://martin.hoppenheit.info/blog/2015/set-windows-proxy-with-powershell/
$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
$settings = Get-ItemProperty -Path $reg
$port = 10809
# select the gateway ip address from the default route and extract the ip address
$gateway = Get-NetRoute |
where {$_.DestinationPrefix -eq '0.0.0.0/0'} |
select { $_.NextHop }
$gateway = $gateway -replace '.*?(\d+\.\d+\.\d+\.\d+).*', '$1'
$cmd = "netsh winhttp set proxy ${gateway}:${port}"
# $cmd
Set-ItemProperty -Path $reg -Name ProxyServer -Value "${gateway}:${port}"
Set-ItemProperty -Path $reg -Name ProxyEnable -Value 1
Invoke-Expression $cmd # need admin privilege
# convert to exe with https://apps.microsoft.com/store/detail/powershell-to-exemsi-converter-free/XPDCHZH119SRT8
$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $reg -Name ProxyEnable -Value 0
netsh winhttp reset proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment