21

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

DextrousDave
  • 6,603
  • 35
  • 91
  • 134
  • Here's another very interesting and brilliant approach to toggle the setting while updating live your icon to indicate the setting, like a little desktop app/button or something: https://stackoverflow.com/a/26708451/4561887 – Gabriel Staples Jun 21 '17 at 01:18

8 Answers8

22

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
PA.
  • 28,486
  • 9
  • 71
  • 95
  • 1
    i tried this command in a bat file: `reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f` now I guess it is changing the reg values successfully but I am not able to open certain pages which I should be able to. When I open the preferences in chrome the proxy is indeed disabled. – Shekhar Joshi Jun 24 '15 at 13:04
  • 2
    for clarity this is what i do, i run the bat, i try to refresh a page(chrome is already open! it throws the proxy error), i open the preferences and see that proxy has been disabled, i re refresh the page the page is loaded this time. – Shekhar Joshi Jun 24 '15 at 13:12
  • 2
    Apparently, it does uncheck the proxy checkbox at Internet Settings, but the system does not take the changes. It unchecks it "visually", but the option still needs to be triggered (as if you clicked the Accept button). – ElPiter Oct 31 '18 at 15:25
  • BEWARE that by modifying the registry doesn't make it instantaneously renew the proxy settings! Such viable options are by using [wininet.dll API](https://learn.microsoft.com/en-us/windows/win32/wininet/about-wininet). You can see such powershell implementation in [IE Enable/Disable Proxy Settings via Registry answered by @user6730445](https://stackoverflow.com/a/39079005/6458107) or by [@YOUNG answered on Sep 19 '19](https://stackoverflow.com/a/58004248/6458107) below. – Unknown123 Sep 26 '19 at 04:50
16

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):
  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:

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...

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 2
    A correction is needed. Add the line: `ProxySettings_path = "C:\Users\" + username + "\Proxy Settings"` under the line `username = WSHShell.ExpandEnvironmentStrings("%USERNAME%")` and remove the hard coded path. – afxentios Aug 10 '18 at 06:46
  • I wish this were a downloadable .exe or similar. Is there at least a github for this? – Gulzar May 25 '21 at 16:47
  • @afxentios, thank you! Great find! I fixed it on my GitHub repo now (link at top of my answer). – Gabriel Staples May 25 '21 at 23:00
  • 1
    @Gulzar, done! I just made a GitHub repo for it. See link at the top of my answer, and [here](https://github.com/ElectricRCAircraftGuy/Windows_Proxy_Toggler). The .vbs script is executable, so you literally just download my repo into your root user folder, and double-click the script to "install" and run it. That's it! It automatically adds the desktop icon. From that point on, you just double-click the desktop icon it created. Note: I don't use Windows at all anymore, so please test it out really quickly and tell me if it works. – Gabriel Staples May 25 '21 at 23:00
  • 1
    This is so cool :) Thanks! Need this often! – jim1427 Aug 04 '22 at 04:48
11

Turn proxy on and off with a .vbs

This .vbs MS Script Determine current proxy setting and toggle to oppisite setting very handy if you want to turn proxy on and off

Option Explicit 
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")

'Determine current proxy setting and toggle to oppisite setting
strSetting = wshshell.regread("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If strSetting = 1 Then 
NoProxy
Else Proxy
End If

'Subroutine to Toggle Proxy Setting to ON
Sub Proxy 
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
End Sub

'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy 
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End Sub
BOB
  • 273
  • 3
  • 9
  • Not working for me in Windows 8.1. Not sure why. When I click my .vbs file nothing happens, and I don't know how to get it to open in a command prompt or anything to debug it. No errors I can see. – Gabriel Staples Jun 21 '17 at 00:22
  • 1
    Update: it DOES work actually...it just takes ~10 sec for the proxy change to take effect is all, and it will NOT update live in the Internet Options --> "Connections" tab --> "LAN Settings" dialog box live when you change the setting from any script. – Gabriel Staples Jun 21 '17 at 01:15
  • Also, here's another very interesting and brilliant approach to toggle the setting while updating live your icon to indicate the setting, like a little desktop app/button or something: https://stackoverflow.com/a/26708451/4561887 – Gabriel Staples Jun 21 '17 at 01:16
  • 1
    @BOB, I took it one step further. See my answer here: https://stackoverflow.com/a/44752679/4561887. I have it creating a desktop shortcut and modifying the icon according to the Proxy state, as well as bringing up a 1 sec timed popup windows to announce the new state each time you click the desktop shortcut. Thanks for your work. You were half the equation. – Gabriel Staples Jun 26 '17 at 03:25
  • Any way to check or uncheck "Use automatic configuration Script" via registry? – Vivek Apr 23 '21 at 04:56
9

Internet explorer and Google chrome both shares same proxy settings. So if we change setting in internet explorer, then it also effects in Google chrome. We can change proxy setting from CMD (command line prompt).

Disable proxy setting:

@ECHO OFF

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f

Enable proxy setting:

@ECHO OFF

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d address:portNumber /f

address: New proxy address
portNumber: Port Number

Save the commands in a batch file and execute it. It will disable/enable the proxy setting for browser.

I found this answer at: http://langbasics.blogspot.in/2012/11/disable-or-enable-proxy-for-internet.html

Andrew NS Yeow
  • 341
  • 2
  • 8
Vikky Jain
  • 91
  • 1
  • 1
  • 8
    While this changes the setting, that setting doesn't take affect for me until I open the properties and click ok. How do I make them actually take effect? – Tony L. Aug 10 '15 at 14:31
1

Disable the proxy

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f

Enable the proxy

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^ /v ProxyEnable /t REG_DWORD /d 1 /f

Set the proxy

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^ /v ProxyServer /t REG_SZ /d ProxyServerIP:Port /f

EKNATH KULKARNI
  • 394
  • 1
  • 5
  • 13
1

Thanks the answer from @Gabriel Staples https://stackoverflow.com/a/44752679/6070417

Just do as the steps first,

but there is two things should to be watched:

1, Like @afxentios said in comment:

A correction is needed. Add the line: ProxySettings_path = "C:\Users\" + username + >"\Proxy Settings" under the line username = >WSHShell.ExpandEnvironmentStrings("%USERNAME%") and remove the hard coded path.

Fix Steps

a) Put this line to toggle_proxy_on_off.vbs under line 26:

ProxySettings_path = "C:\Users\" + username + "\Proxy Settings"

b) Remove line 18 ProxySettings_path = "C:\Users\Gabriel\Proxy Settings".

2, You will see the script indeed update the registry, but it won't work until you open/close IE once. So I found the answer here: https://stackoverflow.com/a/39079005/6070417

Fix Steps

a) Copy the script blow and save to Refresh-System.ps1

function Refresh-System
{
  $signature = @'
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
'@

$INTERNET_OPTION_SETTINGS_CHANGED   = 39
$INTERNET_OPTION_REFRESH            = 37
$type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
$a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
$b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
return $a -and $b
}
Refresh-System

b) Put file Refresh-System.ps1 to "C:\Users\YOUR_USERNAME\Proxy Settings"

c) Add this line to toggle_proxy_on_off.vbs under "End If"(about line 35)

WSHShell.run("powershell -windowstyle hidden -file """ + ProxySettings_path + "\Refresh-System.ps1""")

The script will work without IE.

.

But now when vbs script call powershell script, the powershell window will appear a short moment.

Who knows how to set the powershell window never show? please add a comment.

fish
  • 2,173
  • 2
  • 13
  • 18
  • How do you make the function above into one line? I want to run it in batch file as `powershell.exe -Command "stmnt1; stmnt2; ..."` – Unknown123 Sep 20 '19 at 12:43
  • @Unknown123 Maybe you can use semicolons, see this answer:https://stackoverflow.com/a/22258701/6070417 – fish Sep 23 '19 at 01:45
  • I tried to join them altogheter as `powershell -command "$signature = @'[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);'@; $INTERNET_OPTION_SETTINGS_CHANGED = 39; $INTERNET_OPTION_REFRESH = 37; $type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru; $a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0); $b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0);"` – Unknown123 Sep 24 '19 at 07:12
  • But it returned an error, it sayid the bracket `[` is the culprit. `At line:1 char:16 + $signature = @'[DllImport(wininet.dll, SetLastError = true, CharSet=C ... + ~ No characters are allowed after a here-string header but before the end of the line. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx ception + FullyQualifiedErrorId : UnexpectedCharactersAfterHereStringHeader` It hig – Unknown123 Sep 24 '19 at 07:13
  • I'm new to powershell, haven't understood yet the `@' '@`statement https://stackoverflow.com/q/363884/6458107 – Unknown123 Sep 24 '19 at 07:22
  • @Unknown123 Sorry, I am also not so good at powershell, just see the answer from here:https://stackoverflow.com/a/39079005/6070417 and copy it. – fish Sep 26 '19 at 01:50
  • Yeah I've already asked a new question and it got answered here https://stackoverflow.com/q/58109538/6458107 – Unknown123 Sep 26 '19 at 14:34
0

I wrote a script with "enable/disable proxy" options and that starts as Administrator. you just need to copy it to a file.bat:

@echo off
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B
:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------


@echo off
Setlocal EnableDelayedExpansion


:MAIN_M
ECHO
ECHO 

ECHO 0. QUIT
ECHO 1. ENABLE PROXY 10.10.10.10:8181
ECHO 2. DISABLE PROXY


set /p choice=CHOISE....    


if ´%choice%´==´0´ goto EXIT_M
if ´%choice%´==´1´ goto ENABLE_PROXY
if ´%choice%´==´2´ goto DISABLE_PROXY

:EXIT_M
exit

:DISABLE_PROXY
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
goto MAIN_M

:ENABLE_PROXY
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d 10.10.10.10:8181 /f
goto MAIN_M   
psycho
  • 41
  • 1
  • 3
0

Try this to disable the proxy. PA's accepted answer changes the values in the registry, but for changes to take effect, as noted in the comments, it is necessary to restart IE.

Save the next content as cmd- or bat-file and run it:

REM turn off proxy server setting
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f

REM restart IE for changes to take effect
start /w /b iexplore.exe
taskkill /f /im iexplore.exe

If you would like to toggle proxy to "on" (or 1), you could do:

REM turn on proxy server setting
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f

REM you would likely also want to specify your proxy server and port 
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d ADDRESS:PORT /f

REM restart IE for changes to take effect
start /w /b iexplore.exe
taskkill /f /im iexplore.exe

This was checked on Windows 7 and Windows 10. See more here and here.

antaki93
  • 704
  • 7
  • 10