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

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

Spacer Hey, Scripting Guy! Answer Script 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

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:

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


No Comment.