How To Backup Windows Registry with VB Script | JScript | command line
(C) Kevin Languedoc (klanguedoc)
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 commandline method and also using VBScript. This is handy in a 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);
More by this Author
run WMI queries and process or another DOS or Windows process from SQL Server using the SQL xp_cmdshell function. This tutorial walks you through how to setup xp_cmdshell on the server and execute differenttype sof...
How to Develop an Excel App using the VSTO Interop services (com) and C#. This hub also demonstrates how to add a ribbon and interact with the app from the ribbon.
A free and complete software development proposal template with budget, ROI and CAGR formulas and instructions. Detailed instructions to help you prepare a winning software development proposal.