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

1  Comment 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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Richard Siddaway
    [...] We recently looked at reading Registry values. http://itknowledgeexchange.techtarget.com/powershell/reading-registry-values/ [...]
    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: