Your basic ITPro blog... What's going on at work, what I'm interested in.

Tuesday, June 24, 2008

2008-06-24 PowerShell Exercise

Today we dabble with the Registry...

Of course, I don't have to warn you, use caution when messing with the Registry!

And, with that out of the way, let's look at our Hey, Scripting Guy! question for the day.


Hey, Scripting Guy! I am trying to change a registry value that has the name (Default). I can change any registry value that has a “real” name, but I can’t figure out how to change one with the name (Default). If I export the value the name shows up as @, but that doesn’t work in my script, either. How do I change a registry value named (Default)?
-- TL


   1: Const HKEY_CURRENT_USER = &H80000001
   3: strComputer = "."
   5: Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
   7: strKeyPath = "ScriptCenter"
   8: strValueName = ""
   9: strValue = ""
  11: objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue


As we see, the VB solution is not too complicated... only seven lines of actual code. But, this is a great question that allows us to explore the power of PowerShell as it relates to the Registry. You see, the cool thing is, PowerShell pretty much treats the Registry as any other file system. So, you can use commands like:

  • dir, ls (Get-ChildItem)
  • cd (Set-Location)
  • md, rd (New-Item, Remove-Item)

Using these, along with the various <verb>-ItemProperty cmdlets, we can easily navigate the Registry and add/remove/change/copy keys and values.

So, to address the question and solution listed above, we find the following PowerShell one-liners very handy:

First off, I didn't have the key they are referring to, namely "ScriptCenter" in HKEY_CURRENT_USER. So, let's create that:

New-Item -Path "HKCU:\" -Name "ScriptCenter"

Now, to set '(Default)' with an actual value:

Set-ItemProperty -Path "HKCU:\ScriptCenter" -Name "(Default)" -Value "New Value"

You can verify that this is set with:

Get-ItemProperty -Path "HKCU:\ScriptCenter" -Name "(Default)"

Or, to see all of the values in the key, simply leave off the -Name:

Get-ItemProperty -Path "HKCU:\ScriptCenter"

Of course, all of these one-liners can be shortened further by using aliases, positional values, etc. But, I like complete listings for these examples. So, have fun (WITH CARE) exploring the registry using PowerShell!

No comments:

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332