Guy’s Scripting Ezine 64 – Writing to the Registry

Contents for Ezine 64 – Writing to the Registry

 ♣

This Week’s Secret

It’s an open secret that I have few disclaimers on my site.  So when I suddenly say, watch out, be careful with the registry, then I hope that you will sit up and take notice.  In my experience people who edit the registry go through these 5 stages:

  • Fear of the new registry language
  • Awe at the power of Regedit
  • Complacency – I can do anything
  • Panic
  • Respect for the Registry

Now I never thought that I would reach the Panic stage with Regedit.  Guy thought that he knew it all, then one day I deleted a whole hive, a huge chunk of the registry.  When the machine rebooted, Windows started, but the machine was like a lame duck.  If only I had exported the registry I could have repaired it.  So, even ‘Gung-ho Guy’ now exports the registry before undertaking a new project.

The secret of adding new keys and values with .RegWrite is to practice with harmless registry keys before embarking on more dangerous but exiting projects.

Mission to write keys to the registry

You may have noticed a pattern with my scripts.  Each script has two parts, a clear practical goal, and a VBScript learning point.  This week the goal is to add a welcome message to the registry, the result will display in the Logon dialog box.  While the learning point is to master the .RegWrite method.  (You may remember that we experimented with .RegRead in ezine 62.)

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example – To write information into the registryVBScript to add the Welcome message to Winlogon Logon Box

I want to begin with a registry change that does not require a reboot. The goal of this script is to add a welcome message to the logon dialog box.  To save logging off, you can see the very same message by pressing Ctrl Alt Delete.  When the Windows Security dialog box appears, read the top bar and see your strMessage.

The learning rationale is to practise writing to the registry, but only by experimenting with a risk-free areas.  The tactics are to create a new key called ‘Welcome’, then assign it a string value, for example, ‘This is Guy’s Kingdom’.  Before we examine the .RegWrite syntax, you may notice that WScript creates a Shell object – objShell.

The .RegWrite method has three parts (arguments):

  1. The name of the registry Key that we are creating or writing to.  Note the slashes " \ ".  In particular notice that there is a " \ "  after Winlogon\, but not after Winlogon\Welcome.
  2. The value itself e.g. "Guy’s Kingdom"
  3. The registry key type, e.g. REG_SZ (string) or REG_DWORD.

Example: objShell.RegWrite "HKLM\SOFTWARE\Microsoft\"  _
&  "Windows NT\currentVersion\Winlogon\Welcome", "Guy’s Kingdom", "REG_SZ"

In the actual script I control "HKLM\SOFTWARE\Microsoft\…." with the variable strWinlogon

Another minor point is that because the key is so long, that I need an underscore _ and an ampersand & so that WScript sees my code as one complete line.

Instructions

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g. Welcome.vbs
  3. Double click and then check the message box
  4. You may like to run Regedit and navigate from the HKLM to the winlogon section
  5. Export the Registry before you make any changes

‘ Welcome.vbs
‘ Example VBScript to write to the registry.
‘ Author Guy Thomas https://computerperformance.co.uk
‘ Version 1.3 – February 20th 2005
‘ —————————————-‘

Option Explicit
Dim objShell
Dim strMessage, strWelcome, strWinLogon

‘ Set the string values
strWelcome = "Welcome" ‘ New Key
strMessage = "You are entering Guy’s kingdom"
strWinLogon = "HKLM\SOFTWARE\Microsoft\" _
& "Windows NT\currentVersion\Winlogon\"

‘ Create the Shell object
Set objShell = CreateObject("WScript.Shell")

‘ These are the two crucial command in this script.
objShell.RegWrite strWinLogon & strWelcome, 1, "REG_SZ"
objShell.RegWrite strWinLogon & strWelcome, strMessage, "REG_SZ"

‘ I like a confirmation message
Wscript.Echo "Press Ctrl, Alt Del read top of Menu = " & strMessage

‘ The line below was purely for testing
‘ objShell.RegDelete strWinlogon & strWelcome
WScript.Quit

‘ End of script.

Learning Points

Note 1:   Here is what creates the Welcome key: objShell.RegWrite strWinLogon & strWelcome, 1, "REG_SZ".

Note 2:  I found the commas a little tricky. One comma after the registry key (strWinLogon & strWelcome,) and another after the value (1,).

Note 3:  At first my script failed because I omitted to put speech marks around the "REG_SZ".

Note 3:  The use of variables, strWinLogon, strWelcome and strMessage.

Note 5:  This script uses Microsoft\Windows NT\ section of the registry not the Microsoft\Windows\ folder.

Note 6:  Regedit is the utility for understanding and troubleshooting this script.  In particular Regedit can ensure that you have the correct spelling, once you locate the registry key, then go to File (Menu), Export.  Now you are all set to copy what ever is in the Selected Branch dialog box.  Naturally, you paste this registry string into your script.

See Windows 8 registry.

Challenges

I have commented out a line which uses .RegDelete so that you can see how to remove entries from the registry.  However, I say again – be careful.  Make sure that you do not delete the whole of Winlogon.  Incidentally, if you do remove the ‘ comment, then run the script, you will see your strMessage, but when you dismiss the WScript.Echo message box, the script will continue executing and delete the strWelcome.

Ideally I would like to add more exciting, but potentially dangerous settings to the registry.  If you feel that you have mastered this .RegWrite method, then I am sure that you could research other keys to add to your registry.

Summary – Writing to the Registry

If you need to regularly add new keys and values to the registry, then VBscript is a great solution.  The key is to practice on innocuous settings just to master the .RegWrite methods and syntax.  Make sure you export the registry before you undertake major projects with .RegWrite. 

See more about registry scripts

Registry  • Win Registry Hacks  •PowerShell Registry  • Ezine 8 Registry  • Ezines  •Tool Kit

Ezine 62 Registry  •Ezine 63 Registry  • Ezine 64 Write Registry  • Ezine 119 Registry shortcuts 

Ezine 121 CachedLogons  • Ezine 136 remove shortcut  •Ezine 137 Remove shortcut