Tips, tricks, techniques, tools, and code for those who administer Windows desktops.

VBScript to Determine if a Registry Key/Value Exists

Periodically, you may need to be able to identify within a script that a particular registry key or value exists.  The following code will help you do that.  By supplying the path to the registry key, the function will return a True/False value corresponding to whether it can find and read the key or not.  A true value means the key in question was found and readable by the user running the script.  A false value means the key was either not found or could not be read for some other reason.

'
' This function returns a "true/false" response if a
' given Windows Registry key exists.
'
' Since a script will generate an error if it attempts
' to read a non-existent Windows Registry key, we use
' a local "on error resume next" to keep executing
' normally if the error occurs.
'
Function RegEntryExists(theEntry)
  On error resume next
  set shell = CreateObject("WScript.Shell")
  entry = shell.RegRead(theEntry)
  If Err.Number <> 0 then
    Err.Clear
    RegEntryExists = FALSE
  else
    Err.Clear
    RegEntryExists = TRUE
  end if
End Function

Enhanced by Zemanta

Sponsored Links