The VBScript Network and Systems Administrator's Cafe

Jun 6 2008   1:53PM GMT

A simple way to Write to the registry with VBScript

Jerry Lees Jerry Lees Profile: Jerry Lees

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 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


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(“”)

      Key = objRegistry.RegWrite(RegPath, Value, RegType)
      WriteReg = Key
End Function

Function ReadReg(RegPath)
      Dim objRegistry, Key
      Set objRegistry = CreateObject(“”)

      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 ( File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

 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.

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:

Share this item with your network: