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

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

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:

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:

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:

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:

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:

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.

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?

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.

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

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?

Hi, thank you very much for your script lines, but I have one question: How could the checkbox item "Bypass proxy servers for local addresses" be turned on and off if ProxyOverride is a REG_SZ? It should be a DWORD, isn´t it?

Using ProxyOverride as a REG SZ, it will write the strings in the dialog box in the advanced window, but it will not check the "Bypass proxy servers for local addresses".

I use a batch script to update these settings but they appear to be ignored by the browser. Even opening a new window doesn’t make any difference. The only time I can force the change is by going through the proxy settings in the browser. Any thoughts
on what is happening here?