Share via


How to read a registry key and its values (VBScript)

Hi all, welcome back,

Today I'll share with you a couple of VBScript samples I developed the other day. They use WMI and its StdRegProv class to read the Windows registry.

This sample will take a registry key and show its subkeys and the values within those subkeys:

 ' Constants (taken from WinReg.h)
'
Const HKEY_CLASSES_ROOT   = &H80000000
Const HKEY_CURRENT_USER   = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS          = &H80000003

Const REG_SZ        = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY    = 3
Const REG_DWORD     = 4
Const REG_MULTI_SZ  = 7

' Chose computer name, registry tree and key path
'
strComputer = "." ' Use . for current machine
hDefKey = HKEY_LOCAL_MACHINE
strKeyPath = "SOFTWARE\Microsoft\Cryptography\Defaults\Provider"

' Connect to registry provider on target machine with current user
'
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

' Enum the subkeys of the key path we've chosen
'
oReg.EnumKey hDefKey, strKeyPath, arrSubKeys

For Each strSubkey In arrSubKeys

  ' Show the subkey
  '
  wscript.echo strSubkey

  ' Show its value names and types
  '
  strSubKeyPath = strKeyPath & "\" & strSubkey
  oReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes

  For i = LBound(arrValueNames) To UBound(arrValueNames)
    strValueName = arrValueNames(i)
    Select Case arrTypes(i)

      ' Show a REG_SZ value
      '
      Case REG_SZ          
        oReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue
        wscript.echo "  " & strValueName & " (REG_SZ) = " & strValue

      ' Show a REG_EXPAND_SZ value
      '
      Case REG_EXPAND_SZ
        oReg.GetExpandedStringValue hDefKey, strSubKeyPath, strValueName, strValue
        wscript.echo "  " & strValueName & " (REG_EXPAND_SZ) = " & strValue

      ' Show a REG_BINARY value
      '          
      Case REG_BINARY
        oReg.GetBinaryValue hDefKey, strSubKeyPath, strValueName, arrBytes
        strBytes = ""
        For Each uByte in arrBytes
          strBytes = strBytes & Hex(uByte) & " "
        Next
        wscript.echo "  " & strValueName & " (REG_BINARY) = " & strBytes

      ' Show a REG_DWORD value
      '
      Case REG_DWORD
        oReg.GetDWORDValue hDefKey, strSubKeyPath, strValueName, uValue
        wscript.echo "  " & strValueName & " (REG_DWORD) = " & CStr(uValue)                

      ' Show a REG_MULTI_SZ value
      '
      Case REG_MULTI_SZ
        oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues                               
        wscript.echo "  " & strValueName & " (REG_MULTI_SZ) ="
        For Each strValue in arrValues
          wscript.echo "    " & strValue 
        Next

    End Select
  Next

Next

But what if we only need to know if a registry key exists? We could just do the following:

 ' Constants (taken from WinReg.h)
'
Const HKEY_CLASSES_ROOT   = &H80000000
Const HKEY_CURRENT_USER   = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS          = &H80000003

' Chose computer name, registry tree and key path
'
strComputer = "." ' Use . for current machine
hDefKey = HKEY_LOCAL_MACHINE
strKeyPath = "SOFTWARE\Microsoft\Cryptography\Defaults\Provider"

' Connect to registry provider on target machine with current user
'
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

' Try to enum the subkeys of the key path we've chosen. We can't if the key doesn't exist
'
If oReg.EnumKey(hDefKey, strKeyPath, arrSubKeys) = 0 Then
  wscript.echo "Key exists!"
Else
  wscript.echo "Key does not exists!"
End If

 

I hope this helps.

Cheers,

 

Alex (Alejandro Campos Magencio)

Comments

  • Anonymous
    February 04, 2009
    This worked for me.I had to add in 'On Error Resume Next' though or it would error on subkeys with only the Default entry inside.I actually used it to get a list of installed software, much faster than using WMI's WIN32_Product.
  • Anonymous
    September 30, 2012
    i want how to get version of registry using vb script
  • Anonymous
    October 27, 2012
    This code looks like VB not VBScript!
  • Anonymous
    October 28, 2012
    Hi GR,This code is indeed VBScript.
  • Anonymous
    November 03, 2012
    So why don't you write it as a useful function that accepts a key and returns a value?For example:  function GetResitsryKey(Key){}Get into the good habit of creating functions and procedures not just loose code dumps!
  • Anonymous
    November 04, 2012
    Hi GR,Feel free to post your own sample using functions/procedures and share it with the community if you think that will help even more people.Thank you!
  • Anonymous
    February 18, 2013
    Hi,Is there any chance that you can modify the script to write results in a file in place of the msg screen ?thanks in advance for your help.
  • Anonymous
    July 22, 2013
    The comment has been removed
  • Anonymous
    March 05, 2014
    I will be attempting to save the data on a file (may be a table), and if I succeed, I will post it here.Thanks Again Alejandro, your time is highly appreciated,Alvaro
  • Anonymous
    April 17, 2014
    You can do a simple check with RegRead to trap errors checking for errors other than just existence.  This would have helped Ken with the permissions issue.There are many ways you can do this probably more gracefully.  "RegistryKey" below represents the variable holding the full key path you are reading.Dim WSOSet WSO = CreateObject("WScript.Shell")WSO.RegRead RegistryKeySelect Case Err
    Case 0:    '  Successful read of the key, so do something here    Wscript.echo "Reg key exists..."Case &h80070002:    '  Trapped an error, do something here    Wscript.echo Err.descriptionCase Else:    '  Some other error condition, not trapped above    Wscript.echo "Unexpected error"
    End Select
  • Anonymous
    April 23, 2015
    Worked like a champ.  Thank you very much for the information.