Script to Enable/Disable proxy

PowerShell Script

Today I was working with one application where I needed to test application settings multiple times with proxy on and with proxy off. I wanted to have a script that I can run to change between this 2 states and I also wanted to know last state of proxy (if it’s on or off). I decided to write PowerShell script (as I do for almost all my problems :D).

First thing I done, was to find correct registry key. It is under Curent User hive: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections. Binary value that changes this setting is named DefaultConnectionSettings. We need to look for a value of 9th byte:

Decimal value | Hexadecimal value –> Description

  • 1 | 01 –> All unchecked
  • 3 | 03 –> Use a Proxy Server…” (2) checked
  • 9 | 09 –> “Automatically detect settings” (8) checked
  • 11 | 0b (1+8+2) –> “Automatically detect settings” (8) and “Use a Proxy Server…” (2) checked
  • 13 | 0d (1+8+4) –> “Automatically detect settings” (8) and “Use Automatic configuration script” (4) checked
  • 15 | 0f (1+8+4+2) –> All three check box are checked

PowerShell Script:

$RegKey = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
$Settings = (Get-ItemProperty -Path $RegKey).DefaultConnectionSettings
 
if ($Settings[8] -eq 1) {
    $Settings[8] = 3
    Set-ItemProperty -path $regKey -name DefaultConnectionSettings -value $Settings
    msg console /time:3 "Proxy is now enabled"
}
 
elseif ($Settings[8] -eq 3) {
    $Settings[8] = 1
    Set-ItemProperty -path $regKey -name DefaultConnectionSettings -value $Settings
    msg console /time:3 "Proxy is now disabled"
}
 
else {
    $Settings[8] = 3
    Set-ItemProperty -path $regKey -name DefaultConnectionSettings -value $Settings
    msg console /time:3 "Proxy is now enabled"
}

So, how does the script work? It gets DefaultConnectionSettings value from $RegKey and store it in $Settings. Then it checks for value in 9th byte:
– If it’s 1 (all checkboxes are unchecked), it changes to 3 (proxy on) and other way around (from 3 to 1).
– If the value is different, it changes to 3 (proxy on).

For all situations we get 3 second message with proxy status.

4 Comments

  1. Thanks for sharing!

    Reply
  2. Nice, thanks

    Reply
  3. Worked like a charm. Thank you so much

    Reply
  4. Thanks for the script
    do you have a script without the entry: address and port ?
    i add the value in Advanced
    and activate Automatically detect settings ?

    Reply

Your email address will not be published.