Exporting Settings to Files
Exporting all or part of the registry is something that IT professionals and power users do often. By exporting, I mean copying portions of the registry to another file, typically a REG file, though hive files are more useful. This is a great way to back up settings so you can easily restore them later, if necessary. It's also a good way to share settings with other users or computers. I often create REG files for settings I prefer so that I can change those settings simply by importing a REG file rather than clicking my way through the Windows user interface—one double-click replaces a hundred clicks.
In the IT world, exporting settings to REG files has practical purposes, too. The first is deployment. REG files are the simplest and often the only way to deploy some settings with Windows. You can deploy REG files through your Windows answer file, for example, as you will learn in Chapter 14, “Deploying with Answer Files.” It's also a convenient way to deploy settings from an intranet or help desk. REG files are also an easy way to add settings to your Office 2003 Editions deployment. You can do this through the Custom Installation Wizard found in the Microsoft Office 2003 Editions Resource Kit. Chapter 17, “Deploying Office 2003 Settings,” describes how to add REG files to an Office 2003 Editions installation.
Regedit exports settings to four different types of files: registration, Win9x/NT4 registration, hive files, and text files. The differences between the four are significant, and you learn about them later in this chapter. Follow these steps to export branches of the registry to files:
Click the key at the top of the branch you want to export.
On the File menu, click Export to display the Export Registry File dialog box, shown in Figure 2-5.
In the File Name box, type a name for the file you're creating.
Select one of the following options, depending on the export range you want:
To back up the entire registry, select the All option.
To back up the selected branch, select the Selected Branch option.
In the Save As Type drop-down list, click the type of file you want to create: Registration Files (*.reg), Registry Hive Files (*.*), Text Files (*.txt), or Win9x/NT4 Registration Files (*.reg).
Click Save.
Figure 2-5 Make sure you select the file format you want to use, regardless of the file extension you type in the File Name box.
Importing a file into the registry is similar to opening a file. Click File, Import; in the Files Of Type drop-down list, click the type of file that you're importing; then, in the File Name box, type the path and name of the file you're importing. The following sections describe each of the file types that you see in the Save As Type and Files Of Type drop-down lists. Each type is a different file format and is thus suited to different purposes than the other types are.
Registration Files
Registration files are version 5 (Unicode) REG files—plain text files that look similar to INI files. Each section name represents a key, and each item in a section represents a value. Listing 2-2 is a sample of a version 5 REG file:
Listing 2-2 Sample Version 5 REG File
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Sample] "String"="Jerry Honeycutt" "Binary"=hex:01,02,03,04,05,06,07,08 "DWORD"=dword:00004377 "Expandable String"=hex(2):25,00,55,00,53,00,45,00,52,00,00,00 "MultiString"=hex(7):48,00,65,00,6c,00,6c,00,6f,00,00,00,00 [HKEY_CURRENT_USER\Sample\Subkey]
The most important thing to know about version 5 REG files is that they are Unicode, and some programs can't handle Unicode REG files properly. And because these files are Unicode, each character in the REG_EXPAND_SZ and REG_MULTI_SZ values is two bytes wide. In Listing 2-2, you'll notice this in the values called Expandable String and MultiString. For example, the letter A is 0x0041, not 0x41. For more information about Unicode-encoded text, see Chapter 1, “Learning the Basics.” Windows 2000, Windows XP, and Windows Server 2003 are the only Microsoft operating systems that support version 5 REG files.
Earlier in this section, you learned how to import REG files using Regedit. You can also double-click a REG file to merge it into the registry. Regedit will prompt you to merge the settings that the file contains into the registry and, after you click Yes, it will tell you when it's finished. If you're deploying a REG file to users, however, you don't want them to see the message or answer the prompt, so you'll use Regedit's /s command-line option to run the program quietly. For example, use the command line regedit settings.reg /s from batch files, scripts, answer files, or even from the Custom Installation Wizard found in the Microsoft Office 2003 Editions Resource Kit. For more information about creating and deploying REG files, see the following chapters:
Chapter 11, “Scripting Registry Changes,” describes the format of each value type in REG files and shows you how to build them manually.
Chapter 14, “Deploying with Answer Files,” describes how to deploy REG files as part of your Windows answer file—a great way to deploy user settings.
Chapter 17, “Deploying Office 2003 Settings,” describes how to deploy REG files as part of your Office 2003 customizations.
CAUTION
Don't import a REG file created in one version of Windows into another version—at least not without thinking about it carefully. For example, exporting hardware settings from the Windows NT 4.0 registry and importing them into the Windows XP or Windows Server 2003 registry will likely wreak havoc on either operating system. Some settings are fine to share across Windows versions, however, such as file associations in HKCR and some programs' settings. Use common sense.
Win9x/NT4 Registration Files
Win9x/NT4 registration files are version 4 (ANSI) REG files, which Windows 95, Windows 98, Windows Me, and Windows NT 4.0 support. Listing 2-3 is a version 4 ANSI REG file. The settings are the same as those in the version 5 Unicode REG file that you saw in the preceding section:
Listing 2-3 Sample Version 4 REG File
REGEDIT4 [HKEY_CURRENT_USER\Sample] "String"="Jerry Honeycutt" "Binary"=hex:01,02,03,04,05,06,07,08 "DWORD"=dword:00004377 "Expandable String"=hex(2):25,55,53,45,52,00 "MultiString"=hex(7):48,65,6c,6c,6f,00,00 [HKEY_CURRENT_USER\Sample\Subkey]
Instead of Unicode text, version 4 files are ANSI text files. That means that each character is a single byte wide. The letter A is 0x41. You notice the difference between this and the earlier Unicode REG file in the Expandable String and MultiString values. Characters in the REG_EXPAND_SZ and REG_MULTI_SZ values are single bytes, which is more intuitive for most people. This is the file format that's compatible with programs expecting ANSI REG files, and it has the added benefit of being compatible with earlier versions of Regedit.
Hive Files
Hive files are binary files that contain portions of the registry. As you recall from Chapter 1, “Learning the Basics,” Windows stores different parts of the registry in different hive files. Regedit displays all these hives together in one logical unit. Hive files are useful tools, though. You can export branches to hive files that can then be imported to another computer or by another user. They're great backups.
Exported hive files have purposes similar to those of REG files. Hive files have most of the advantages of REG files, except that you can't view and edit them in a text editor. Also, they can't target individual settings. The advantage that hive files have over REG files is that you can load and edit them in Regedit without actually replacing your own settings. The section “Working with Hive Files,” later in this chapter, describes how to load hive files.
Choosing Between REG and Hive Files
Regedit exports branches to four different file formats. Each format has strengths and weaknesses that make it appropriate for some tasks and useless for others. This section should help you choose the right format each time.
Exporting to hive files is my choice most of the time. I like hive files because they're much more accurate than either type of REG file. They are the same format as the Windows working hive files, so they represent settings in exactly the same way. Also, when you import a hive file, Regedit deletes the branch it's replacing before importing the settings. In other words, the editor removes any settings that exist in the working registry but do not exist in the hive file you're importing. When restoring keys from a backup after an unsuccessful registry edit, this is exactly the behavior you want. Hive files have another strength that makes them my choice most of the time: you can load them as new hives and view their contents without affecting other parts of the registry. The only drawback to this is that you can't view them in Notepad.
Although hive files are usually my choice, there are a few scenarios that require me to use REG files. The first is when I'm working with programs that don't understand hive files. For example, the Custom Installation Wizard found in Microsoft Office 2003 Editions Resource Kit can read REG files but not hive files. The second scenario is when I'm exporting settings to different versions of Windows. Windows 98 doesn't provide a way to load hive files. Finally, and importantly, when I'm trying to track down a setting in the registry by comparing snapshots, comparing two hive files isn't feasible, but comparing two REG files is easy using Microsoft Word 2002.
Text Files
You can export keys to text files, but you can't import keys back into the registry. If you're curious about what an exported text file looks like, take a look at the sample printer output in the “Printing the Registry” section earlier in this chapter. Exported text files and printer output are one and the same. Regedit makes exported text files more readable than REG files are, which can help you interpret settings better, but that's about the only use of text files.