What, no HKCR in PowerShell?

If you’ve been working with PowerShell for registry access, you’ve probably noticed by now that something is missing.

PowerShell has two registry ‘drives’ defined:

HKLM for HKEY_LOCAL_MACHINE

HKCU for HKEY_CURRENT_USER

The other registry roots are not defined.

A few days ago, a friend of mine was trying to find and remove some entries from HKEY_CLASSES_ROOT and was having a hard time using RegEdit.

So, I offered my help, while promoting my latest obsession – PowerShell.

Imagine my embarrassment when I found out a minute later that there is no HKCR drive defined in PowerShell.

Fear not, my friends, for the HKLM and HKCU are merely there for convenience. 

You can define by yourself, with a simple command, any registry drive you want.

So, before working on the HKCR drive, we simply define it like this:

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT

Now, you have full access to HKEY_CLASSES_ROOT via HKCR.

Have fun.

 

P.S.

While on this subject, Ever noticed how you can’t use ‘-filter’ with the ‘dir’ command on registry drives?

Well, you can pipe the results through the question mark filter to do the same job, though I’m quite sure there’s some performance penalty. Still, better than the old RegEdit.exe, right?

Here’s an example:

cd HKLM:\SOFTWARE\

dir | ?{$_.Name -like *Int*}

Published in: on June 8, 2008 at 7:55 Comments (1)

Consuming weather WebService with PowerShell

Update: Added the code in text form, after the jump.

Man, you’ve gotta love PowerShell.

Although I’m mostly into GUI and user experience, I just love console applications.

They’re super fast, powerful, concise, and if written correctly they are also easy to use (there’s that user experience theme again).

If I had to sum up the benefits of PowerShell in one sentence it would be this:

“Object oriented, interpreted, .net enabled , well structured shell and scripting environment.”

To install Windows PowerShell, just go here.

Now have a look at this cool little script I wrote last night.

It uses Yahoo! weather web service to get the local weather (in this example, local means Israel’s international airport, which happens to be close to where I live).

Here’s a snapshot of the script inside PowerGui so you can see all the nice color coding, the actual text is after the jump.

script source code

Click on the image above to open it full-size and let’s go over the lines real quick.

Line 2: creates a new .NET WebClient object.

Line 3: calls Yahoo’s WebService by downloading the result xml, converting it to a .NET XmlDocument object on the fly.

Lines 5,6,12,13,21-25: get a string value out of an xml node or attribute.

Some other interesting things to consider:

  • Lines 8,14 and 26 use the -f formatter, which works basically like C#’s String.Format().
  • Also note the pipeline (|) used to manipulate the text color in these lines.
  • To get the weather someplace else, all you need to do is modify the url, see their instruction page for more info.

And this is how it looks when I run it:

(more…)

Published in: on April 28, 2008 at 14:43 Comments (3)