How Can I Create a New Registry Key?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a new registry key?

— DL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DL. First of all, for those of you who get all your news from the Hey, Scripting Guy! column (as if there was any other source for news and information?) we are happy to report that the University of Washington men’s basketball team defeated a very good Northern Iowa squad 70-61 to win the Basketball Travelers Classic. Admittedly, it wasn’t always pretty: the Huskies are an incredibly young team, occasionally putting 5 freshmen on the floor at the same time. And the officiating was … interesting … to say the least: the refs allowed the players to shake hands before the game started, but that was pretty much the only time any contact was allowed. (We think a couple fouls were even called because a player “violated someone’s personal space.”) But it was fun, and while they are apt to experience growing pains along the way, the Huskies should be a pretty good team come NCAA tournament time.

What’s that? Other news and information? No, as far as we know the Washington-Northern Iowa game was pretty much the only thing of interest going on in the world.

Trivia note. In case you’re wondering, it’s the Northern Iowa Panthers. Of course, the Scripting Guy who writes this column already knew that, just as he knew that it’s the Pepperdine Waves and the Nicholls State Colonels. Depending on how you want to look at it, that’s either very impressive (Delaware Fighting Blue Hens, anyone?) or kind of sad.

Wait, hold everything: we’ve just been handed an important news flash! Sources tell us that this is how you create a new registry key:

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Script Center"
objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath

According to a spokesperson for the Scripting Guys – who asked that his name not be used – the script starts out by defining a constant named HKEY_CURRENT_USER and setting the value to &H80000001; this results in the new registry key being created in the HKEY_CURRENT_USER portion of the registry. What if you wanted to create the new registry key in HKEY_LOCAL_MACHINE? No problem; just change the first line of code to look like this, and replace all instances of HKEY_CURRENT_USER in the script with HKEY_LOCAL_MACHINE:

Const HKEY_LOCAL_MACHINE = &H80000002

After defining the constant we next connect to the WMI service on the local computer. And sure, you can create a registry key on a remote machine; just assign the name of that remote computer to the variable strComputer. Something like this:

strComputer = "atl-fs-01"

One thing to watch out for: make sure you specify \root\default when connecting to the StdRegProv class. In other words, don’t use \root\cimv2, like you do for most WMI scripts. Why not? That’s easy: the StdRegProv class doesn’t live in \root\cimv2; it’s housed in \root\default instead.

Believe it or not, at this point we’re almost done. Our next chore is to assign the registry path for the new registry key. We want to create a key named SOFTWARE\Script Center in HKEY_CURRENT_USER; therefore, we assign SOFTWARE\Script Center to a variable named strKeyPath:

strKeyPath = "SOFTWARE\Script Center"

That’s a good point. It’s not surprising that we can create a key named SOFTWARE\Script Center; after all, the SOFTWARE key already exists. But what if we wanted to create a key named SOFTWARE\Script Center\My Registry Keys\Test Key? Do we need to create a Script Center key, then create a My Registry Keys key, and then, finally, create a Test Key key?

Fortunately, the answer is no. Instead, just assign the entire path to the variable strKeyPath:

strKeyPath = "SOFTWARE\Script Center\My Registry Keys\Test Key"

When you run the script WMI will automatically create any and all the required keys and subkeys.

Speaking of which, to actually create the new registry key we simply call the CreateKey method, passing as parameters the registry hive (HKEY_CURRENT_USER) and the path to the new key (strKeyPath):

objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath

Just like that, we’ve got ourselves a brand-new registry key.

You know, we’ve got a few minutes here, so why don’t we take our new registry key out for a test spin; after all, there’s no point in having a registry key if you don’t get to do fun things like create new registry values in that key. Here’s a sample script that adds a new DWORD value – B – to our new registry key, assigning My DWORD Value the value 13:

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Script Center"
strValueName = "My DWORD Value"
dwValue = 13
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

We won’t explain this script in any detail today; for more information on working with the registry, see the Microsoft Windows 2000 Scripting Guide. For now, we’ll just note that we can create a new registry value simply by specifying the path to that value and then calling – in this case – the SetDWORDValue method. If the registry value in question already exists the script will modify that value; in this example, that means setting the value to 13. If the registry value doesn’t exist then the script will automatically create the new value and then set it to 13. Could there be anything easier? Maybe, but we haven’t found it yet. (And trust us, we Scripting Guys are always on the lookout for easy things.)

Don’t want to create a DWORD value? No problem; just use one of these methods to create a different type of registry value:

•

SetStringValue

•

SetExpandedStringValue

•

SetMultiStringValue

•

SetBinaryValue

Hope that helps, DL. You say you have another question? Doesn’t the University of Washington also have a football team, one that – five weeks into the season – looked like they were finally going to turn things around and return the Huskies to the college football prominence they enjoyed for so long? Sadly, the answer is no: as near as we can tell, the UW no longer has a football team. Thank goodness for basketball, huh?

0 comments

Discussion is closed.

Feedback usabilla icon