Backing Up the Registry

Microsoft® Windows® 2000 Scripting Guide

With the proper precautions, you can edit the registry without creating unrecoverable problems. Nevertheless, it is possible to make changes to the registry that will prevent Windows from loading, and force you to reinstall the operating system. However, if you are careful about making backups, a number of recovery options will be available to you should a problem arise.

Assuming you have a proven backup procedure in place, however, making registry changes is no more dangerous than installing new hardware devices. If you replace your video card and Windows will not start, you can simply remove the new video card and reinstall the old card. Likewise, if you make a change to the registry and Windows will not start, you can simply remove the new registry and reinstall the old registry.

Back up regularly

Back up your current working registry regularly. One incorrect or conflicting value can create many problems, even preventing the operating system from starting.

Saving a backup of the registry on removable media or in secure shared folders can help you restart a computer even if an error prevents you from accessing files on your hard disk.

Back up before direct editing

The Registry Provider methods bypass standard safeguards, allowing settings that can degrade performance, damage your system, or even require you to reinstall the operating system. Some settings are incompatible with others, forcing you to reload your original settings to restore system performance. Unless you have backed up the correct settings, restoring them can prove very difficult, especially if you have made other changes to the registry or if some changes are preventing you from starting Windows.

It should be noted that there is nothing inherently harmful with the Registry Provider; the caution is warranted simply because the Registry Provider requires you to know, in advance, the acceptable values for a registry entry. For example, proper computer operation could depend on a registry entry whose value is set to either 0 or 1. A command-line or graphical configuration tool would typically allow you to set this entry only to 0 or 1. The Registry Provider, by contrast, might allow you to set this to 2, a value that might create problems.

Back up before installing new hardware or software

Back up your registry before you install new hardware or software, and before you remove old hardware or software. This precaution allows you to restore the system to its previous configuration if the changes conflict or degrade performance.

You can create scripts that use the Reg.exe tool (which ships as part of the Windows 2000 Support Tools) to generate backups of the registry and to restore those backups. To make your changes effective after restoring the registry, restart your computer.

Before relying on your registry backup process, perform a practice backup and restore on a noncritical system.

Scripting Steps

Listing 16.2 contains a script backs up the registry. To carry out this task, the script performs the following steps:

  1. Use the CreateObject method to create a WshShell object and store a reference to the object in a variable.

  2. Use the Exec method of the WshShell object to invoke the Reg.exe tool to back up HKLM/System to a file named sw.hiv.

Listing 16.2 Backing Up the Registry

  
1
2
Set objShell = CreateObject("WScript.Shell")
objShell.Exec "%comspec% /k reg.exe SAVE HKLM/System sw.hiv"

You can use the Reg.exe command-line tool itself instead of invoking it within a script. The advantage of writing a script is that you can then write a second script that uses the WshController object (see "WSH Primer") to run the backup script on computers throughout your enterprise. If necessary, you can use the Reg.exe command-line tool to restore a saved registry hive.