Searching for a String within a Registry Key
Strings and Registry Keys
Software programs write data to strings within a computer's registry keys. A VBScript programmer can take advantage of this to determine the state of a program prior to executing further instructions within their code. This can be accomplished by accessing the WScript.Shell object within the Windows Script Host.
The registry is full of information such as hardware, software and user settings. Although a programmer should use caution when working with the registry, especially if one is attempting to write to the registry or make changes, a programmer can write a VBScript that can read these strings of data within the registry to determine everything from operating system level to what license server an application is pointing to. The options are endless .
Things You'll Need Windows OS (98 or Newer)
String Search VBScript Code
Create Test Key and String
Go to “Start”-->”Run”, type “regedit” in the “Open:” box, then select “OK”. This will bring up the Registry Editor.
Right-click on "HKEY_CURRENT_USER" and select “New”-->”Key”. This will create a new key with an edit box ready for the input of a new name. Name this new key “Test”.
Select the new “Test” key. Right-click and select “New”-->”String Value”. This will create a new string value with an edit box ready for the input of a new name. Name this new string “Test”.
Create VBScript File
Create a VBScript file by going to “Start”-->”All Programs”-->”Accessories”-->”Notepad”. Save this file as “C:\Temp\DoesKeyExist.vbs”. Replacing the .txt file extension with a .vbs file extension creates an executable VBScript file.
Copy the following code. Do this by moving the mouse cursor just before the “o” in “option explicit”. Hold down the left mouse button and drag the mouse cursor just past the “)” in “WScript.Quit()”.
Dim WshShl, regKey, RegRead
Set WshShl = WScript.CreateObject("WScript.Shell")
regKey = WshShl.RegRead ("HKCU\Test\Test")
If regKey <> "Test" then
wscript.echo "Key does not exist."
Paste the code into the DoesKeyExist.vbs file created in the first step. Do this by selecting the open DoesKeyExist.vbs file. Right-click in the white space on the first line of the file and select “Paste”.
Save the DoesKeyExist.vbs file and exit Notepad.
Run the VBScript and Look at Message
Run the DoesKeyExist.vbs script. Do this by browsing to “C:\Temp\DoesKeyExist.vbs” using My Computer or Windows Explorer. My Computer is located at “Start”-->”My Computer”. Windows Explorer can be accessed by right-clicking on “Start” and selecting “Explore”.
Double-click on the DoesKeyExist.vbs file. A message box stating the word “Test” will appear. Select “OK”. This shows that the string within the key created earlier does exist and that the “WshShl.RegRead” portion of the VBScript is reading that value and displaying it using the “wscript.echo regKey”.
Run the VBScript Again After Changing Registry
Go back into the registry as shown earlier and double click on the “Test” string. This will open a window with the word “Test” under “Value data:”. Delete the word “Test” in this field and exit the registry. Double-click on the DoesKeyExist.vbs file. This will create a condition where the string within the key does not exist. The “WshShl.RegRead” portion of the script will reflect this and display a message stating that the “Key does not exist.”
More by this Author
Don’t overlook the power of this scripting language! Administrators, developers, analysts and others have all discovered the power of Microsoft VBScript. VBScript, sometimes called “VBScripting” is a...
Applications make use of environment variables either by writing to or reading from them at different times during the runtime of a program. System administrators, IT analysts and anyone else using VBScript, can take...
Learn how to get rid of the musty, mildew-y smell coming from your window AC unit and stop the stink!
No comments yet.