Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch File to disable internet options proxy server

In Windows, I want to disable the Proxy Server setting in Internet Options by using a batch Script. What command can I use to do this?

If unsure what I am referring to, see

Internet Properties > Connections > LAN Settings >Proxy Server

Thank you

like image 889
DextrousDave Avatar asked Aug 26 '13 08:08

DextrousDave


2 Answers

It's in the registry, under [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]

you can either use the REG command in your BAT, or prepare a couple of .REG files, to automate the changes.

for example, to disable Proxy, try

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
like image 129
PA. Avatar answered Sep 22 '22 04:09

PA.


Update 25 May 2021: get the icons and latest script from my GitHub repo here now: Windows_Proxy_Toggler. See the super-simple installation instructions there too.


Here is a way using a simple .vbs script as a "widget" type desktop shortcut. The first time you want to run the script, click the .vbs file you create. This will automatically generate a desktop shortcut for you with an appropriate icon. Each time you click the shortcut thereafter it toggles the proxy setting, brings up a timed popup box for 1 sec to tell you whether the proxy is ON or OFF now, and changes the shortcut icon to an ON or OFF symbol to indicate the new proxy state.

File: "C:\Users\YOUR_USERNAME\Proxy Settings\toggle_proxy_on_off.vbs"

'Toggle your Proxy on and off 
'Gabriel Staples - www.ElectricRCAircraftGuy.com
'Written: 21 June 2017 
'Updated: 25 June 2017 
'References: 
' 1) https://stackoverflow.com/a/27092872/4561887 
' 2) https://stackoverflow.com/a/26708451/4561887 
' Timed message boxes:
' - *****https://technet.microsoft.com/en-us/library/ee156593.aspx
' - https://stackoverflow.com/questions/14105157/automatically-close-msgbox-in-vbscript
' Debug output:
' - ex: Wscript.Echo "here is your message"

Option Explicit 

'Variables & Constants:
Dim ProxySettings_path, VbsScript_filename 
ProxySettings_path = "C:\Users\Gabriel\Proxy Settings"
VbsScript_filename = "toggle_proxy_on_off.vbs"
Const MESSAGE_BOX_TIMEOUT = 1 'sec; change this value to set how long the message box displays when you toggle the proxy setting 
Const PROXY_OFF = 0

Dim WSHShell, proxyEnableVal, username 
Set WSHShell = WScript.CreateObject("WScript.Shell")
'get the username string for use in path names, since trying to use the "%USERNAME%" variable directly in path names throws an error
username = WSHShell.ExpandEnvironmentStrings("%USERNAME%") 

'Determine current proxy setting and toggle to opposite setting
proxyEnableVal = wshshell.regread("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If proxyEnableVal = PROXY_OFF Then 
  TurnProxyOn
Else
  TurnProxyOff
End If

'Subroutine to Toggle Proxy Setting to ON
Sub TurnProxyOn 
  'turn proxy on via a registry entry 
  WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
  'create/update desktop shortcut 
  CreateOrUpdateDesktopShortcut("on")
  'notify user via an auto-timed popup box 
  WSHShell.Popup "Internet proxy is now ON", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub

'Subroutine to Toggle Proxy Setting to OFF
Sub TurnProxyOff 
  'turn proxy off via a registry entry 
  WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
  'create/update desktop shortcut 
  CreateOrUpdateDesktopShortcut("off")
  'notify user via an auto-timed popup box 
  WSHShell.Popup "Internet proxy is now OFF", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub

'Subroutine to create or update a shortcut on the desktop 
Sub CreateOrUpdateDesktopShortcut(onOrOff)
  'create a shortcut 
  Dim shortcut, iconStr
  Set shortcut = WSHShell.CreateShortcut("C:\Users\" + username + "\Desktop\Proxy On-Off.lnk")
  'Set the target path (target file) to run when the shortcut is clicked 
  shortcut.TargetPath = ProxySettings_path + "\" + VbsScript_filename
  'Set the working directory. This is necessary in case you ever make this shortcut call a batch (.bat) file, for instance, which in turn calls a .vbs script. In order to know where the .vbs script file/command is located, the shortcut must be operating in the working directory where the .vbs scripts are located. Otherwise, calls to the .vbs scripts from a .bat file this shortcut points to, for instance, won't work since their directories are not in the Windows %PATH% variable, and you'll get an error which states: "'name_of_vbs_script_file' is not recognized as an internal or external command, operable program or batch file."
  shortcut.WorkingDirectory = ProxySettings_path 
  'Set the icon to associate with this shortcut 
  If onOrOff = "on" Then
    iconStr = "on.ico"
  ElseIf onOrOff = "off" Then
    iconStr = "off.ico"
  End If 
  shortcut.IconLocation = ProxySettings_path + "\Icons\" + iconStr
  'Save the shortcut 
  shortcut.Save
End Sub 

Instructions:

  1. Create a folder called "C:\Users\YOUR_USERNAME\Proxy Settings"
  2. Create the "toggle_proxy_on_off.vbs" file, as shown above, in that folder.
  3. Create an "Icons" folder here: "C:\Users\YOUR_USERNAME\Proxy Settings\Icons"
  4. Download the following two .png images (update: dead links--get the icons and script from my GitHub repo now instead):
    • ON icon image: http://s30.postimg.org/sgoerz0od/image.png
    • OFF icon image: http://s13.postimg.org/9zha38zkj/off.png
  5. Convert those images to icons (.ico files) using http://icoconvert.com/, for example. Choose File (choose a .png from above) --> Upload --> choose "ICO for Windows 7, Windows 8, Vista and XP" format --> click "Convert ICO" --> click "Download your icon(s)"
    • Save the ON icon as "C:\Users\YOUR_USERNAME\Proxy Settings\Icons\on.ico"
    • Save the OFF icon as "C:\Users\YOUR_USERNAME\Proxy Settings\Icons\off.ico"
  6. Now, double-click the "C:\Users\Gabriel\Proxy Settings\toggle_proxy_on_off.vbs" file to run it. It will automatically create a "Proxy On-Off" shortcut file on your desktop, with the appropriate icon to indicate whether the Proxy is ON or OFF.

From this point on, just click the "Proxy On-Off" desktop shortcut directly to toggle the Proxy on and off.

Here's what it looks like when the Proxy is OFF:

enter image description here

Here's what it looks like when the Proxy is ON:

enter image description here

Here's an example of the 1-second popup window that comes up whenever you click the shortcut icon to toggle the Proxy on/off.

enter image description here

References:

  1. Batch File to disable internet options proxy server <-- taught me how to use a .vbs script to toggle the Proxy on and off
  2. Windows desktop widget to turn proxy on and off <-- taught me the ingenious technique on how to make a .vbs script act like a widget by creating a Windows shortcut and changing its icon each time you click on it
  3. Timed message boxes:
    • *****https://technet.microsoft.com/en-us/library/ee156593.aspx
    • Automatically close MsgBox in vbscript?

Todo:

Can someone please help me figure out how to enhance this one step further by making it change the icon name each time too?--ie: instead of saying "Proxy On-Off" on the shortcut, have it say "Proxy is ON" or "Proxy is OFF", according to its current state. I'm not sure how to take it that one step further, and I've put enough time into it for now...

like image 20
Gabriel Staples Avatar answered Sep 25 '22 04:09

Gabriel Staples