How Can I Switch Between Using a Proxy Server and Not Using a Proxy Server?

How Can I Switch Between Using a Proxy Server and Not Using a Proxy Server?

  • Comments 17
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! At work we need to use a proxy server to connect to the Internet; when I take my laptop home to work, however, I don’t need to use a proxy server. I know I can use the Internet Options in Control Panel to change between using a proxy server and not using a proxy server; is there any way to switch between the two using a script?

-- EA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, EA. Let’s hope there’s a way to do this using a script; if there isn’t, this column will be much shorter than usual. (Of course, many people might argue that that wouldn’t be such a bad thing.) If you aren’t sure what EA is talking about, he’s talking about the bottom set of options (the options in the section labeled Proxy server) in the following dialog box:

Proxy Server Settings


This, by the way, is a fairly common scenario: lots of companies use proxy servers on their internal network, but few Internet Service Providers use proxy servers for their consumer networks. If you use a laptop computer both at home and at work, you’ll constantly find yourself switching between using a proxy server and not using a proxy server.

So how can we do this using a script? Well, as it turns out there are three registry values that control these settings. (Yes, from the dialog box it appears as though there are four settings that need to be controlled. Don’t worry; we’ll explain how this all works.) These three registry values - found in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings key in the registry - include:

ProxyEnable. This DWORD value configures the Use proxy server for your LAN check box. If ProxyEnable is set to 1, then use of a proxy server is enabled (i.e., the check box is checked). If ProxyEnable is set to 0, then Internet Explorer will not use a proxy server.

ProxyServer. This REG_SZ (string) value is used to configure both the Address and Port settings. To do this the proxy server address and port must be passed in this format: address:port. In other words, to set myproxy as the address of the proxy server and port 80 as the port you assign this value to ProxyServer: myproxy:80.

Incidentally, if you disable the use of a proxy server you do not have to clear this registry value; this value is used only when Internet Explorer is using a proxy server. This makes things nice for script writers: to disable the proxy server you only have to set the value of ProxyEnable to 0; you can leave both ProxyServer and ProxyOveride as-is.

ProxyOverride. This REG_SZ value configures the Bypass proxy server for local addresses check box. To instruct Internet Explorer to bypass the proxy server for local addresses set this value to <local>. To instruct Internet Explorer not to bypass the proxy server for local addresses set the ProxyOverride value to 127.0.0.1.

Having said that, it’s possible that your ProxyOverride value might not be <local>. Before you write your script you might want to use Regedit to verify the values for Bypass proxy server for local addresses when enabled and when disabled.

Got all that? Now that we know the registry values that need to be configured (as well as how to configure them) the rest is as easy as writing a WMI script that enables the proxy server, specifies the proxy server address and port, and tells Internet Explorer to bypass the proxy server for local addresses:

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"

strValueName = "ProxyEnable"
dwValue = 1
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

strValueName = "ProxyServer"
strValue = "svcproxy:80"
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

strValueName = "ProxyOverride"
strValue = "<local>"
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

The script starts off by defining a constant named HKEY_CURRENT_USER and setting the value to &H80000001; we’ll use this constant later on to tell the script we want to work with the HKCU portion of the registry. We then connect to the WMI service, this time binding to the root\default namespace and the StdRegProv class (the WMI class used for reading and writing registry values).

After we make the connection we next assign the registry path (SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings) to a variable named strKeyPath; we then assign the registry value (ProxyEnable) and the configuration value (1) to the variables strValueName and dwValue. We now have all the information we need to call the SetDWORDValue method and change the value of ProxyEnable to 1:

objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

After that we repeat the same process for ProxyServer and ProxyOverride. Note, however, that both of these registry values have a REG_SZ data type; therefore we use the SetStringValue method instead of SetDWORDValue. (If you have no idea what we’re talking about when we say things like SetDWORDValue and SetStringValue then take a look at the Registry chapter in the Microsoft Windows 2000 Scripting Guide.)

As we noted earlier, disabling the proxy server is much easier: all we have to do is set the value of ProxyEnable to 0:

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"

strValueName = "ProxyEnable"
dwValue = 0
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

In fact, after your proxy settings have all been configured (that is, after you’ve initially assigned values to ProxyServer and ProxyOverride), you can toggle back and forth between using a proxy server and not using a proxy server simply by configuring the ProxyEnable key. In other words, after you have a proxy address and port and after you’ve decided what you want to do with the ProxyOverride value then all you need to do is run this script to re-enable use of the proxy server:

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"

strValueName = "ProxyEnable"
dwValue = 1
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

To disable the proxy server just set the value of ProxyEnable to 0. It’s that easy.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • The proxy settings are changed but not applied that way. I need to go to the proxy settings screen and press OK button for the settings to be applied. How can I handle it?

  • I agree with shureek.  The Internet Settings dialog is doing *something* else, like refreshing the IP configuration or something.  Programmatically changing the registry entries is unreliable.  The new settings always display the expected values if I load the Internet Settings dialog, but if I don't, they don't always take effect.

  • I can follow this to set it but as soon as I put an IF statement in to check and switch it the script doesn't seem to work.  Is there something special with an if statement?

  • @shureek, jwismar

    I can find TWO places in my registry where ProxyOverride is being mentioned.

    So... maybe you want to check yours, too?

    I'm dealing with a stupid .reg file, so I couldn't check the codings.

    kr, zara

  • Is there a way to programatically force Internet Explorer to reaload\reread\refresh proxy settings without having to restart the program after adding\changing proxy settings using a vbscript?

  • To answer my own question - newer versions of IE can be run using the "iexplore.exe -noframemerging" switch to allow for new intsanses of IE to read the latest Proxy settings applied.

  • GO cod and black ops lol nvm we are halo fans but we still play cod games =) get back to us on x box live

  • There is a very easy way to sort out proxy settings in for internet. Just a little work for a lot less pain. Setup the proxy in IE. Open up registry setting and search for your proxy. Should be around here somewhere [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]

    Right click on internet settings and export to file called proxyon

    close regedit

    go back to proxy settings in IE and clear the proxy, port, and ticks apply and close

    open up regedit again and you should be at the same location, but the proxy should have gone, export the Internet settings to a file called proxyoff

    move both files to root of c:

    open up notepad and copy into

    regedit /s "c:\proxyon.reg"

    file save as and change file type to all and save as c:\proxyon.bat

    now change to

    regedit /s "c:\proxyoff.reg"

    file save as and change file type to all and save as c:\proxyoff.bat

    close notepad, got to c: drive, create a shortcut from, proxyon.bat, and proxyoff.bat (these are the files that look like they have a cog or wheel in the middle of them, place shortcuts on desktop, hey presto quick proxy change in IE. you get a quick flash from a dos window when it processes. Works a treat, use it for my staff when they use internet at home.

  • @shureek, jwismar, TH

    The *something* else are:

    DefaultConnectionSettings

    SavedLegacySettings

    You have to save it for ProxyON and ProxyOFF settings and recall it when necessary.

  • still have to restart IE to apply changes. Tried both ways (.vbs and .reg) but after switching proxy I have to restart IE and restore last browser session but I cannot simply open a new tab or click on a link on one of the open pages and continue browsing. So a "seamless" proxy switching would be fine. Any way?

  • Brilliant. - I used the <local> in my script and it worked liek a charm.

    Thanks

  • find all the commands ,please let me known the restart of DNS on win 7

  • Gents, you left out one important and annoying setting from the dialogue box.  How can the script be updated to change the setting for "Automatically Detect Settings"?  We have a long and sad history trying to implement WPAD and are looking to force this setting OFF (unchecked).  After a registry comparison, it looks like this setting is embedded within a data stream key called "DefaultConnectionSettings".  Can you please help me figure out how to change this value within this obtuse key?

  • @SaxDaddy

    Since W2K/XP most of us have been using Group Policy to do this.  It is under the "Connection" settings of Internet Explorer Maintenance.  Other advanced auto-configuration and automatic proxy settings are also settable here.

  • Hello, I finally found a way to set the "Automatically detect settings" in vbs. It might be the right or wrong way but it works for me.

    Here is my post about it blogs.msmvps.com/.../programmatically-alter-automatically-detect-settings-in-ie-through-vbs