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!
‘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”)
Temp = ReadReg(“HKCU\Control Panel\Desktop\Wallpaper”)
Temp = ReadReg(“HKCR\.txt\”)
WScript.Echo TempFunction ReadReg(RegPath)
Dim objRegistry, Key
Set objRegistry = CreateObject(“Wscript.shell”)
Key = objRegistry.RegRead(RegPath)
ReadReg = Key
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!