Check if registry key exists with WScript

im trying to check if the registry key exists and no matter what I try, I always get the error message "cannot open the registry key for reading"

im code using:

keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\BOS\\BOSaNOVA TCP/IP\\Set 1\\Cfg\\Sign On\\"; try { var shell = new ActiveXObject("WScript.Shell"); var regValue = shell.RegRead(keyPath); shell = null; } catch(err) { } 

what is missing here?

+4
source share
4 answers

You probably need to remove the trailing slash. If you use this, it will look for the default value for the key you specified, and if it does not find it, it will lead to an error.

Conversely, if you try to access the key as if it were a value without using a trailing slash, you will get the same error.

Some examples of key access attempts:

Fails:

 var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion"; var shell = new ActiveXObject("WScript.Shell"); var regValue = shell.RegRead(keyPath); WScript.Echo("Value: " + regValue); 

Succeeds (but gives an empty result, since the default value is empty):

 var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\"; var shell = new ActiveXObject("WScript.Shell"); var regValue = shell.RegRead(keyPath); WScript.Echo("Value: " + regValue); 

Some examples trying to access a value:

Exceeds ( Value: C:\Program Files output):

 var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir"; var shell = new ActiveXObject("WScript.Shell"); var regValue = shell.RegRead(keyPath); WScript.Echo("Value: " + regValue); 

Fails (do not use trailing slash when accessing the value):

 var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir\\"; var shell = new ActiveXObject("WScript.Shell"); var regValue = shell.RegRead(keyPath); WScript.Echo("Value: " + regValue); 
+7
source

You are trying to open a HKLM hive and probably WScript (or the user you ran it) do not have permissions to do this. You can view permissions using regedt32

+2
source

What i got

If the default value for the key is not set, it will say unable to open registry key '---' for reading.

Now, if the key has a default value and you do not add \\ after the key, then you will get the same error.

So, to get the default value, you must add \\ , otherwise add the exact list of keywords under this key. e.g. Version, Location, etc.

0
source

vbscript with one backslash and trailing slash at the end for a key works for me:

 On Error Resume Next Set WSHShell = CreateObject("WScript.Shell") s = WSHShell.RegRead( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\" ) if Err.Number <> 0 then MsgBox(Err.Description) MsgBox("Office is not installed?" ) exit Function Else MsgBox("Office is installed") exit Function ''wscript.quit End If MsgBox("xxxxxxxxxxxxxxxxx") 
0
source

All Articles