Enterprise Software

Use these scripts to modify a remote registry setting

Learn to use "peek and poke" registry scripts to control advanced registry manipulations.


I have a friend who takes great pleasure in making fun of me for using the Command Prompt in Windows. However, almost anything you can do from the Windows GUI you can do from a command prompt, such as scripting various operations. Granted, scripting is overkill for performing a one time operation on a local machine. If you had to modify the registry, would you rather write and debug a 50-line script, or just open REGEDIT, and click the mouse a few times? However, if you have a lot of machines that need updating, scripting can be a huge timesaver because a single script can be written to update the registries on all the remote machines, saving you from updating the registries manually.

Before I begin
Before I get started, I want to point out that modifying the registry is dangerous business. Making an incorrect modification can destroy Windows and/or your users' applications. Furthermore, the chances of accidentally making an incorrect modification are greatly increased when using a script. Therefore, be sure to back up all PCs involved before running any registry modification scripts. You should also thoroughly test your script before running it on a large scale.

Peeking and poking
When I first got into programming at the age of 15, two terms I used quite a bit were peek and poke. Peek referred to looking at a particular memory address to see what value was contained in that location. Poke was used to insert a value into a particular memory location.

I'm not a professional developer, so I’m not sure if those terms are still in use today. Nonetheless, the terms peek and poke perfectly describe the scripts I am going to show you how to build. One script, which I will call the peek script, looks at a particular location within the registry and returns the current value of that registry key.

The other script, which I will refer to as the poke script, inserts a value into a registry key. The poke script can also create a registry key if it does not already exist. For this example, I will enable a screen saver via a script. I will use peek code within the script to look and see if the screen saver is already enabled. If it isn’t, then I will run a poke script to enable it. Of course, the two scripts can also be combined into one script.

The peek script
Let’s begin by looking at the peek script. The script below will check to see whether or not the screen saver is enabled. The script will return a value of 1 if the screen saver is enabled and a value of 0 if it is not:
On Error Resume Next
HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
ValueName = "ScreenSaveActive"
    objReg.GetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue
If IsNull(strValue) Then
    Wscript.Echo "Verify whether or not a screen saver is being used:  The value is either Null or could not be found in the registry."
Else
    Wscript.Echo "Verify whether or not a screen saver is being used: ", strValue
End If

The first line of this script is On Error Resume Next. This command simply keeps the script from terminating should it encounter an error. The next three lines set the current user and computer. Right now these lines of code are intended to look at the screen saver settings on the local machine for the user currently logged in. These lines could be easily modified to look at a remote machine or an entire group of remote machines.

Where things really start getting interesting, though, are in the following lines:
strKeyPath = "Control Panel\Desktop"
ValueName = "ScreenSaveActive"

As you can see, the strKeyPath command contains a partial registry path. When the script executes, this path is appended to HKEY_CURRENT_USER for a complete path. I will show you how that works in a moment. The ValueName command tells the script which specific registry key the script should be looking at. In this case, the key is ScreenSaveActive.

The objReg.GetStringValue command tells the script to look at the designated spot within the registry and return the string value that is contained within that location. You will notice that this command is followed immediately by the absolute value HKEY_CURRENT_USER and then by the strKeyPath, which has been assigned the value Control Panel\Desktop. The command is then followed by the parameters ValueName, which was assigned the value ScreenSaveActive, and strValue, which is an empty container for holding the returned string value. When the script processes this command, it looks like this:
objReg.GetStringValue HKEY_CURRENT_USER\Control Panel\Desktop\ScreenSaveActive, strValue

The next line that the script runs checks the value returned. If the script runs strValue and the value is null, then it will display an error message indicating the value is either null or couldn’t be found in the registry. Assuming that this message hasn’t been displayed, the script’s final line displays a 1 if the screen saver is enabled or a 0 if it is not.

The poke script
Below is the poke script that can be used to enable the Windows screen saver by inserting a value of 1 into the ScreenSaveActive key. As you look at the script below, you will notice it is similar to the peek script that I have just shown you.
HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
ValueName = "ScreenSaveActive"
strValue = "1"
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue

This script goes through almost the exact same initial setup as the peek script does. The only major differences between the two scripts are found in the objReg command. In the peek script, the objReg command was followed by a period, the command GetStringValue, the registry path, the registry key name, and a variable that could hold the value returned by the command.

The poke script uses this same basic syntax, but introduces two new objReg commands, CreateKey and SetStringValue. The objReg.CreateKey command is used to create the path that the registry key will be inserted into. The syntax is objReg.CreateKey path. In this case, the script would see the command as objReg.CreateKey HKEY_CURRENT_USER\Control Panel\Desktop. Granted, this path should already exist, but before you try to insert or modify any registry keys, you should make sure that the path exists to avoid errors. When this command runs, if the path already exists, the command won’t hurt anything. If the path doesn’t exist though, it will create the path, thus preventing the script from causing an error.

The objReg.SetStringValue command is used for actually setting the value of a particular registry key. The syntax for this command is identical to that used for the objReg.GetStringValue command. The only difference is that rather than reading the current value and inserting it into the strValue variable, it will insert the contents of the strValue variable into the registry key. You will notice that in the poke script the line just before the objReg.SetStringValue command sets the strValue to 1, in this case telling the script that the screen saver should be enabled.

Tweakomatic
To get some more ideas of what you can do with registry scripts, go to the Microsoft Web site and download Tweakomatic. Tweakomatic is a utility that helps you to build scripts for manipulating many different registry values. You can use it to alter all sorts of cosmetic and security settings within Windows and Internet Explorer.

Editor's Picks