Hack 70 Safely Edit the Registry Using .reg Files


Forgo the dangers and inconvenience of editing the Registry directly. Instead, use plain-text .reg files.

When you're editing the Registry, it's easy to make small errors that cause major repercussions. You may inadvertently edit the wrong key, put in a wrong value, or?given how confusing the Registry is?even make changes without realizing it. The Registry is unforgiving when this happens. It doesn't keep a backup, so you're stuck with the new setting unless you've made backups yourself, as outlined in [Hack #71].

When you edit the Registry directly, you're also apt to make errors if you're making multiple changes, because you have no chance to look at all the changes you're making at once.

There's a way to solve both problems: use .reg files to edit the Registry. These are plain ASCII text files that you can create or read with Notepad or any text editor and that you merge into the Registry to make changes. You can create a .reg file from scratch, or you can export it from a portion of the Registry, edit it with Notepad or another text editor, and then merge it back into the Registry. You'll find that .reg files are particularly useful if you're going to make changes to the Registry of several computers or if you are leery about editing the Registry directly.

You should also consider creating .reg files to copy the parts of the Registry that you're about to edit using the Registry Editor. Then, if you make a mistake with the Registry Editor, you can revert to the previous version of the Registry by merging the .reg file into the Registry. They're also useful if you need to do search-and-replace operations on parts of the Registry, because the Registry Editor doesn't include search-and-replace functionality. You can do the search-and-replace operation in your text editor and then merge the edited file back into the Registry.

To create a .reg file from an existing portion of the Registry, run the Registry Editor, highlight the key or portion of the Registry that you want to export, and choose File-> Export. Choose a name and location for the file. You can export an individual key, a branch of the Registry, a hive, or the entire Registry. Following is an example a .reg file exported from the HKEY_CURRENT_USER\Control Panel\Accessibility branch:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Control Panel\Accessibility]
[HKEY_CURRENT_USER\Control Panel\Accessibility\Blind Access]
[HKEY_CURRENT_USER\Control Panel\Accessibility\HighContrast]
"High Contrast Scheme"="High Contrast Black (large)"
[HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Preference]
[HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Response]
[HKEY_CURRENT_USER\Control Panel\Accessibility\MouseKeys]
[HKEY_CURRENT_USER\Control Panel\Accessibility\SerialKeys]
[HKEY_CURRENT_USER\Control Panel\Accessibility\ShowSounds]
[HKEY_CURRENT_USER\Control Panel\Accessibility\SoundSentry]
[HKEY_CURRENT_USER\Control Panel\Accessibility\StickyKeys]
[HKEY_CURRENT_USER\Control Panel\Accessibility\TimeOut]
[HKEY_CURRENT_USER\Control Panel\Accessibility\ToggleKeys]

Edit a .reg file as you would any other text file. As you can see, the first line of the .reg file starts with Windows Registry Editor Version 5.00. Don't change this; Windows XP uses it to confirm that the file does in fact contain Registry information. Previous versions of Windows have a different first line; for Windows 95/98/Me and Windows NT 4, the first line reads either REGEDIT4 or Registry Editor 4.

The names of Registry subkeys are surrounded by brackets, and they include the full pathname to the subkey, such as [HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Response] in our example. Following each subkey are the subkey values and data. Values and data are both surrounded by quotation marks. Here is the full section of a subkey, along with its associated values and data:

"Group"="Pointer Class"
"DisplayName"="Mouse Class Driver"

As you can see, quotes surround data for String values. DWORD values, however, are preceded by dword: and don't have quotes surrounding them. Similarly, binary values are preceded by hex: and don't have quotes surrounding them.

Edit the value and data and save the file. When you've made your changes, import the file back into the Registry by choosing File Import in the Registry Editor and opening the file. An even easier way to import it is to double-click on the file. XP will ask whether you want to import it; when you answer yes, XP will import it and make the changes to the Registry. This is somewhat counterintuitive and can be confusing; you may at first think that double-clicking on a .reg file will open it for editing. But it won't; it will merge it into the Registry. To open a .reg file, open Notepad or another text editor and then open the .reg file. Alternatively, you can right-click on the .reg file and choose Edit.

Because double-clicking on a file merges it back into the Registry, it's easy to mistakenly make Registry changes when you really just want to edit a .reg file. To protect yourself against this kind of mistake see later in this hack.

7.4.1 Delete Registry Keys and Values Using .reg Files

You can use a .reg file not just to create new keys or values or modify existing ones, but to also delete keys and values. To delete a key with a .reg file, put a minus sign in front of the key name, like this:

-[HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Response]

When you import the .reg file, that key will be deleted. Keep in mind that you won't be able to delete a key this way unless all of its subkeys have first been deleted, so you'll have to delete them first.

You can also delete a key's value using a .reg file, by putting a minus sign after the equals sign in a .reg file, like this:


When you import this into the Registry, the value will be deleted but the key will still stay intact.

7.4.2 Protect the Registry by Changing the Default Action for Double-Clicking a .reg File

As I mentioned earlier in this hack, when you double-click on a .reg file, the file doesn't open for editing; instead, it gets merged directly into the Registry. This can easily cause serious problems, because you might want to edit the file, and so end up double-clicking on it, the way you normally open files in XP. But the file will end up merging it into the Registry and making Registry changes you didn't want to make.

To solve the problem, you can change the default action so that a .reg file is opened for editing in Notepad rather than merged when you double-click on it. In Windows Explorer, choose Tools Folder Options File Types to open the File Types dialog box. Highlight the REG entry and click Advanced. Highlight the Edit action and click Set Default. The Edit action should turn bold. Click OK.

7.4.3 Change the Default Editor for .reg Files

Notepad is the default editor for editing .reg files, but if you have another text editor you'd rather use you can force that to be the default instead. First, follow the directions from the previous section to open the File Types dialog box and highlight the REG entry's Edit action. Then, click on the Edit button and type in the full path and filename of the text editor you want to use to edit .reg files, followed by %1?for example:

C:\Program Files\TextPad 4\TextPad.exe %1

Then click OK twice.

Never use a word processor such as Word to edit .reg files (unless you make sure to save it as a plain text file from within the word processor!). Word processors add extra codes that the Registry can't understand. Always use a text editor such as Notepad or WordPad.