How to Backup Windows Registry With VB Script or JScript
Backing up the Windows Registry is an accepted best practice before making changes to the Windows Registry. This is quite easy using the RegEdit tool in Microsoft Windows. You can export the whole registry or just one branch. This tutorial will show you how using the command line method and also using VBScript. This is handy in an enterprise environment where you can't go from computer to computer.
Export (Backup) the whole registry (command line)
The Command Line method provides several options to be used in order to interface with the registry. For Exporting a branch or the whole Registry you can one of the following commands
The /e option notifies the regedit tool that is has to export the contents of the registry with or without a branch as follows:
regedit /e c:\reg_backup.reg
This command exports the complete registry to the reg_backup.reg file on the c drive. Note that the file does not have to be a reg file. Regedit can export to Word or a text file or a file of another type like rich text (rtf) also. See the screenshot below.
regedit /L:c:\system.dat /R:c:\user.dat /e c:\backup.reg
This command is similar to the first execept that it is using the system.dat and user.dat files which are the registry repositories.
regedit /e c:\backup.reg HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
This command would export on the specified branch and key. However I strngly urge anyone mofidying the registry to take a full backup because messing up the registry would have dire consequences on the computer's functiionality, even prevented Windows from starting.
Export Registry using VBscript
Another way to export the contents of the Registry is to use VBScript or JScript. The principal is the same. If you are exporting a key then you would need to assign it to a variable as in the example below. Next assign a backup file to another variable like regFile. If you want to store the backups in a folder locally or on the network, make sure the location exists or check to see if it exists beforehand and create folder is needed. To get a handle on the file using VBScript, you will to create a FileSystem Object, like fso, in the provided example. Then check if the file exists assign the regedit command with the /E option and the backup file without a key or with a key. Both examples are provided below. Use the one that best fits your needs. Finally execute the command passing in the regCmd variable.
VB Script Code
oShell = CreateObject("WScript.Shell") regKey = " HKEY_LOCAL_MACHINE\Software\Microsoft\Windows " regFile = "c:\backup.reg" Set oShell = CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") ' do not overwrite an existing file If Not fso.FileExists(regFile) Then 'backup with no key regCmd = "regedit.exe /E regFile 'backup with key regCmd = "regedit.exe /E regFile & """ " & """" & regKey & """" oShell.Run regCmd, 0, True End If
Export the Registry using JScript
JScript can also be used to export the registry contents. The process is similar to SBScript except that syntax is a little different as the following example demonstrates.
Var shell = CreateObject("WScript.Shell"); Var fso = CreateObject("Scripting.FileSystemObject"); regKey = ("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows "); shell.Run ("regedit /e c:\backup.reg" +regKey);