Jun 6 2008 1:53PM GMT
Posted by: Jerry Lees
regwrite,
registry,
regwrite method,
Functions,
wscript.shell
Last time I posted I gave you a function that provided A simple way to read the registry with VBScript, in this posting we’ll use the same Wscript.shell object as last time, just use a different method– the RegWrite Method!
Keep in mind, writing to the registry can be dangerous to the stability of your system. If you do not specifically know you need to write the registry location, and with what value, I recommend not writing to the registry. I have used a made up registry location so running this script should be as safe as I can make it.
This example writes to HKey_Current_User\VBScriptAdmin the key Teststring, which is a REG_SZ (or string) value. You’ll notice that this value likely doesn’t exist before you run the script. The method creates the entire path if it doesn’t exist– which is very nice! (Or not so nice if you typo the path.)
At the end of the script you should see the ReadReg function reads the value back in and writes the value read, which should be Success!.
So, lets look at 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 = WriteReg(”HKCU\VBSriptAdmin\Teststring”,”Success!”,”REG_SZ”)
Temp = ReadReg(”HKCU\VBSriptAdmin\Teststring”)
WScript.Echo Temp
Function WriteReg(RegPath, Value, RegType)
‘Regtype should be “REG_SZ” for string, “REG_DWORD” for a integer,…
‘”REG_BINARY” for a binary or boolean, and “REG_EXPAND_SZ” for an expandable string
Dim objRegistry, Key
Set objRegistry = CreateObject(”Wscript.shell”)
Key = objRegistry.RegWrite(RegPath, Value, RegType)
WriteReg = Key
End Function
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.webstemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!