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

Tuesday, April 8, 2008

2008-04-08 PowerShell Exercise

UPDATE! MoW posted about this same thing here! Must read!

NOTE: MoW (The PowerShell Guy) has lately been doing the same thing that I have been doing here... converting Scripting Guys! scripts to PowerShell solutions. If you are interested in this as all, I would definitely check his Blog out! MoW is some kind of PowerShell Savant! His scripts blow mine away.

This entry, in specific, illustrates this. When I worked through this item, I basically re-wrote the VB solution in PowerShell. As I was doing so, I was thinking, "Rather than transliterating VB scripts in PowerShell, there has to be a 'PowerShell' was of doing this... a completely different methodology." And, if you look at MoW's solution, you will see what I was hinting at. Pretty cool!

For today's entry, we see a pretty radical example of how PowerShell's treatment of the Registry (treating it as just other directory) is so powerful.

QUESTION:

Hey, Scripting Guy! Do you have a script that will enable me to enumerate all the values in a registry key?
-- DG

THE SCRIPTING GUYS' ANSWER:

   1: Const HKEY_CURRENT_USER = &H80000001
   2:  
   3: Const REG_SZ = 1
   4: Const REG_EXPAND_SZ = 2
   5: Const REG_BINARY = 3
   6: Const REG_DWORD = 4
   7: Const REG_MULTI_SZ = 7
   8:  
   9: strComputer = "."
  10:  
  11: Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
  12:  
  13: strKeyPath = "Software\Microsoft\Internet Explorer\Main"
  14: objRegistry.EnumValues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes
  15:  
  16: For i = 0 to UBound(arrValueNames)
  17:     strText = arrValueNames(i)   
  18:     strValueName = arrValueNames(i)
  19:  
  20:     Select Case arrValueTypes(i)
  21:         Case REG_SZ
  22:             objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath, strValueName,strValue 
  23:             strText = strText & ": "  & strValue
  24:         Case REG_DWORD
  25:             objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath, strValueName, intValue    
  26:             strText = strText & ": "  & intValue
  27:         Case REG_MULTI_SZ
  28:             objRegistry.GetMultiStringValue HKEY_CURRENT_USER,strKeyPath, strValueName,arrValues  
  29:             strText = strText & ": "
  30:             For Each strValue in arrValues
  31:                 strText = strText & "   " & strValue 
  32:             Next  
  33:         Case REG_EXPAND_SZ
  34:             objRegistry.GetExpandedStringValue HKEY_CURRENT_USER,strKeyPath, strValueName,strValue    
  35:             strText = strText & ": "  & strValue
  36:        Case REG_BINARY
  37:             objRegistry.GetBinaryValue HKEY_CURRENT_USER,strKeyPath, strValueName,arrValues    
  38:             strText = strText & ": "
  39:             For Each strValue in arrValues
  40:                 strText = strText & " " & strValue 
  41:             Next  
  42:         End Select 
  43:  
  44:     Wscript.Echo strText
  45: Next

MY POWERSHELL ANSWER:

   1: Get-ItemProperty 'HKCU:\software\microsoft\internet explorer\main'

No comments:

Additional Info

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