Check Reg Key using VBScript

email me

' RegKeyExists(strName)
'
' Returns True if a registry key exists and False if not.
'
' strName = Name of the registry key
'
' strName should begin with:
' HKEY_CURRENT_USER (or HKCU),
' HKEY_LOCAL_MACHINE (or HKLM),
' HKEY_CLASSES_ROOT (or HKCR),
' HKEY_USERS, or
' HKEY_CURRENT_CONFIG
 
Function RegKeyExists(strName)
 
Const NO_EXISTING_KEY = "HKEY_NO_EXISTING\Key\"
 
Dim objWsh
Dim strKeyPath
Dim strNoKeyError
 
Set objWsh = WScript.CreateObject("WScript.Shell")
strKeyPath = Trim(strName)
If Right(strKeyPath, 1) <> "\" Then strKeyPath = strKeyPath & "\"
On Error Resume Next
' Get the error description by trying to read a non-existent key
objWsh.RegRead NO_EXISTING_KEY
strNoKeyError = Err.Description
Err.Clear
objWsh.RegRead strKeyPath
' Compare the error description with the previous determined sample
If Replace(Err.Description, strKeyPath, NO_EXISTING_KEY) = strNoKeyError Then
  RegKeyExists = False
Else
  RegKeyExists = True
End If
Err.Clear
On Error Goto 0
Set objWsh = Nothing
 
End Function