Wednesday, July 19, 2006

Edit IE Address Bar with PowerShell

Lately, I've been doing a lot of testing of our web app. And for the last month or so, its all been on the same page. Being a shortcut key addict, I would type the url to the page in IE's address bar and then use F4 and the arrow keys to call it. As long as I kept going to that page, the url would always be at the top of the list. However, if I started going to other sites, the address bar's list would get long and my test url would get lost somewhere inside. It's a real pain the rump to go searching for it.

So I put together this little PowerShell script. It clears the address list and puts in the test urls that I want.

#1
$regpath = "HKCU:\Software\Microsoft\Internet Explorer\TypedURLs"
$xmlpath = "C:\Allen\PSScripts\AddressBarLinkList.xml"
cd $regpath
#2
(Get-Item . ).GetValueNames() |
Where-Object {$_ -match "^url\d+"} |
ForEach-Object { Remove-ItemProperty . $_ }

#3
$xml = New-Object xml
$xml.Load($xmlpath)

#4
$xml.urls.url |
ForEach-object {
New-ItemProperty -Path $path -Name $_.name -type string -value $_.link
}
cd c:

Let's look at each step and see what's going on. In step #1, I create 2 variables, $regpath and $xmlpath, and load them with some paths. Note that the regpath uses the registry key HKCU as a drive. In PowerShell the registry keys are treated equivalent to folders in the File System and registry values are treated equivalent to files in the File System. All you have to do is change directory to HK whatever and your in.

In step #2, we see a string of commands piped into each other. First we get the names of all the values in the key. Next we use Where-Object to pull only those values that match a regular expression. Then I delete all the values in the result set.

Step #3 starts with the creation of a new variable, $xml, of type xml (of the .NET xml's). Pretty much, what I'm doing here is creating a new Xml.Document object. On the next line I call the xml object's Load method to load an xml file from a filepath. The xml file contains a list of urls that I want loaded into the address bar. Take a look:

<urls>
<url>
<name>url1</name>
<link>http://localhost/Application/test.aspx?ItemId=70946</link>
</url>
<url>
<name>url2</name>
<link>http://DevServer/Application/test.aspx?ItemId=70946</link>
</url>
</urls>

In step #4, I get a list of all the url nodes. What is really interesting is the notation I used to get them, "$xml.urls.url". It reads like an XPath statement, just with dots instead of slashes. Next I use the New-ItemProperty command to create a key value for each of the urls in my xml file.

So this script edits the registry, reads an external file and parses an xml document. That's a lot when you consider that it took what amounts to 6 lines of code. I put in some line breaks and formatting to make it easier to read. Also note, that I didn't use any aliases in the script. It could be rewritten as:

cd HKCU:\Software\Microsoft\Internet Explorer\TypedURLs
(gi . ).GetValueNames() | ? {$_ -match "^url\d+"} | % { rp . $_ }
$xml = New-Object xml
$xml.Load("C:\Allen\PSScripts\AddressBarLinkList.xml")
$xml.urls.url | % { New-ItemProperty -P HKCU:\Software\Microsoft\Internet Explorer\TypedURLs -N $_.name -t string -v $_.link }
cd c:

No comments: