9.5. Importing Registry Files

Problem

You want to import changes to the registry using a registry (.reg) file.

Solution

Using a graphical user interface

  1. Open an Explorer (explorer.exe) window.

  2. Browse to the .reg file you want to import.

  3. Double-click on the file.

  4. Click Yes to confirm the import.

  5. You can accomplish the same thing within Registry Editor by going to File Import.

Using a command-line interface

The following command imports a registry key or subkey:

> regedit /s <FilePath>

For example:

> regedit /s c:\rallencorp.reg

Using VBScript

' This code imports the contents of a registry file.
' Since there are no scripting functions to do this, I simply
' shell out to the regedit tool to do it.
   
' strCommand = "regedit /s <FilePath>"
strCommand = "regedit /s c:\rallencorp.reg"
set objWshShell = WScript.CreateObject("WScript.Shell")
intRC = objWshShell.Run(strCommand, 0, TRUE)
if intRC <> 0 then
   WScript.Echo "Error returned from importing registry: " & intRC
else
   WScript.Echo "No errors returned from importing the registry file"
end if

Discussion

Importing a registry file is as easy as double-clicking it, but this is where the danger lies. Be careful anytime you import a registry file; make sure it contains exactly what you want to import. If you accidentally import the wrong file, it can unintentionally overwrite values. Since registry files are text based, open a file in a text editor such as Notepad to see what it contains.

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.