The VBScript Network and Systems Administrator's Cafe

Jun 4 2008   2:57PM GMT

A simple way to read the registry with VBScript



Posted by: Jerry Lees
Tags:
regread
regread method
VBScript
wscript.shell

On occasion when administering a group of systems you need to preform certain tasks depending on if something is installed or a particular setting is a certain value and the only way to determine if a system needs attention is to look in the registry. So, In this installment we will look at reading the registry with the Wscript.shell object.

This is a simple task, as you can see from the function below in the example code. However, it’s something that will save you a ton of time in troubleshooting when you have to first check to ensure a particular system has the setting you need to change or that is creating a problem!

In the example code below, I have attempted to keep it pretty generic in an effort to demonstrate the code on as many systems as possible– so it doesn’t do much because I’ve kept my registry checks limited to components I think should be installed on most all systems. However, it does present the concepts and I’ve added the major registry keys’ abbreviations you would use to reference them as comments in the code.

Additionally, remember that you reference the path all the way to the data you want including the value name, if you are specifying a value. If you reference a registry key, but no value, you will get back the (default) value. Both examples are shown below, I encourage you to break out regedit and take a look at the values I referenced in the code below so you can better see what the code is doing. Also, you can look here to learn more about the regread method of the wscript.shell object.

So, lets get to the code!

Option Explicit
Dim Temp

‘HKEY_CURRENT_USER = HKCU
‘HKEY_LOCAL_MACHINE = HKLM
‘HKEY_CLASSES_ROOT = HKCR
‘HKEY_USERS = HKEY_USERS
‘HKEY_CURRENT_CONFIG = HKEY_CURRENT_CONFIG

Temp = ReadReg(“HKLM\System\CurrentControlSet\Services\lanmanworkstation\DisplayName”)
WScript.Echo Temp
Temp = ReadReg(“HKCU\Control Panel\Desktop\Wallpaper”)
WScript.Echo Temp
Temp = ReadReg(“HKCR\.txt\”)
WScript.Echo Temp
Function ReadReg(RegPath)
     Dim objRegistry, Key
     Set objRegistry = CreateObject(“Wscript.shell”)
     Key = objRegistry.RegRead(RegPath)
     ReadReg = Key
End Function

As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s (www.websystemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

1  Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Throwaway998234234
    "As always, this code works perfectly." Line 15: WScript.Echo TempFunction ReadReg(RegPath) There needs to be a line break between the words Temp and Function.
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: