PowerShell for Windows Admins

May 30 2010   9:47AM GMT

Reading Registry values

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We have seen how to access the registry using the StdRegProv class. Now we look at reading from the registry. If we know the value we want to read out of the registry we can use one of the following classes

GetBinaryValue
GetDWORDValue
GetExpandedStringValue
GetMultiStringValue
GetQWORDValue
GetSecurityDescriptor
GetStringValue

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
$HKCR = 2147483648 #HKEY_CLASSES_ROOT
$HKCU = 2147483649 #HKEY_CURRENT_USER
$HKLM = 2147483650 #HKEY_LOCAL_MACHINE
$HKUS = 2147483651 #HKEY_USERS
$HKCC = 2147483653 #HKEY_CURRENT_CONFIG

$reg = [wmiclass]‘\\.\root\default:StdRegprov’

$key = "SOFTWARE\Microsoft\Windows\CurrentVersion"
$value = "CommonFilesDir"
$reg.GetStringValue($HKLM, $key, $value)  ## REG_SZ

$value = "ProgramFilesPath"
$reg.GetExpandedStringValue($HKLM, $key, $value)  ## REG_EXPAND_SZ

$key = "SOFTWARE\Microsoft\Windows\CurrentVersion\BITS"
$value = "LogFileMinMemory"
$reg.GetDwordValue($HKLM, $key, $value)  ## REG_DWORD

We need to define the constants that define which registry hive we are reading and create a WMI object for the StdRegprov class.  We then set the key and the value we want.  The method of reading is determined by the type of data to be returned as shown in the examples.

The actual value is returned as the svalue or uvalue propert depending on if the data type is string or numeric

2  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • List Registry subkeys - PowerShell for Windows Admins
    [...] We recently looked at reading Registry values. http://itknowledgeexchange.techtarget.com/powershell/reading-registry-values/ [...]
    0 pointsBadges:
    report
  • jsforth777
    THANK YOU!! I had to modify the Get-PendingReboot script so that it just checked or server manager. Through trial and error I was able to get the uValue to compare not equal to zero and works great with your examples and how to read the registry.
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: