Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I clear the list of URLs that appear in the Internet Explorer address bar?

-- GA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, GA. You know, if you’ve ever pondered the question, “What is it that makes the Scripting Guys different from other Microsoft employees?” well, we can save you some time and trouble by just giving you the answer: the Scripting Guys are dumber than other Microsoft employees. For example, it turns out that the list of URLs that appear in the Internet Explorer address bar drop-down list are stored in the registry; to be more specific, they are stored in this registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs

The obvious implication? Delete the values found in that registry key and you’ll delete the list of URLs that appear in the Internet Explorer address bar.

Of course, most Microsoft employees know how dangerous it is to mess around with the registry. For example (and we’re not making this up), when we wrote the Microsoft Windows 2000 Scripting Guide we included a chapter on writing scripts that manipulate the registry. That was fine, but our editors at the time tried to make us include the following disclaimer after each and every script in the chapter:

“Changing the registry with a script can easily propagate errors. The scripting tools bypass safeguards, allowing settings that can damage your system, or even require you to reinstall Windows. It is recommended that you do not use scripts to change the registry.”

In other words, “Here’s a script that changes a value in the registry. Now don’t use it.”

But, like we said, the Scripting Guys are … different … than other Microsoft employees. Without even bothering to ponder the possible consequences we wrote the following script, one that deletes all the registry values in the TypedURLs registry key:

Const HKEY_CURRENT_USER = &H80000001
 
strComputer = "."
 
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Internet Explorer\TypedURLs"

objRegistry.EnumValues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes

For Each strValue in arrValueNames
    objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValue
Next

Was it a bad idea to do that on our actual work computer, as opposed to, say, running the script on a test machine? Probably. But all’s well that ends well: as near as we can tell, not only did this script clear the list of URLs from the address bar, but it didn’t cause any problems along the way, either. As usual, you run scripts that play around with the registry at your own risk, but this one seems to work just fine.

Let’s take a few minutes to walk through the script and talk about how it does work. To begin with, we define a constant named HKEY_CURRENT_USER and set the value to &H80000001; this simply tells the script which registry hive to work with. We then use these two lines of code to connect to the WMI service on the local computer:

strComputer = "."
 
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

And, yes, this script can be modified to work against a remote computer: all you have to do is assign the name of the remote machine to the variable strComputer. For example:

strComputer = "atl-ws-01"

So far, so good, right? After connecting to the WMI service we assign the registry path within HKEY_CURRENT_USER (Software\Microsoft\Internet Explorer\TypedURLs) to a variable named strKeyPath. That brings us to this line of code:

objRegistry.EnumValues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes

As it turns out, each URL that appears in the address bar is stored as a separate registry value: url1, url2, url3, etc. What we’re doing here is using the EnumValues method to retrieve a collection of all the registry values found in the TypedURLs key. We give EnumValues four parameters:

HKEY_CURRENT_USER, the costant that represents the registry hive.

strKeyPath, the variable that indicates the registry key we want to work with.

arrValueNames, an “out” parameter that EnumValues will fill with the names of all the registry values found in TypedURLs.

arrValueTypes, a second out parameter that EnumValues will fill with the registry data type for each value in TypedURLs. For this particular script we don’t care about the data type, but because it’s a required parameter we need to include it.

In other words, after calling EnumValues we’ll get back a collection of all the registry values found in TypedURLs, a collection that will be stored in the variable arrValueNames. That means we can now easily delete each value: all we have to do is set up a For Each loop to walk through the collection and, within that For Each loop, use the DeleteValue method to delete each value. That’s what we do here:

For Each strValue in arrValueNames
    objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValue
Next

As you can see, DeleteValue takes three parameters: the constant HKEY_CURRENT_USER, the variable strKeyPath, and the variable strValue (which simply represents the registry value to be deleted.)

That’s all you have to do. After the registry values have been deleted no items will appear in the Internet Explorer address bar drop-down list. However, as you type new URLs in the address bar new registry values will automatically be created. Over time, you’ll eventually build up a brand-new collection of typed URLs, which you can then delete using this script, ensuring that the great circle of Internet Explorer life goes on.

Note. One little quirk to watch out for. Suppose you delete all the URLs, then open Internet Explorer and type a new address into the address bar. This address will be immediately added to the drop-down list; however, if you look in the registry you won’t see any new values in TypedURLs. For some reason these new URLs aren’t added to the registry until that instance of Internet Explorer has been terminated. If you close Internet Explorer and then check the registry you’ll see your new registry value.

Like we said, this seems to work just fine, despite the fact that many people would caution you against using a script to delete values from the registry. And now, if you’ll excuse us, someone down the hall needs to borrow a pair of scissors. We’re going to run some over to them right now.