How to Import Windows Registry (.reg) Files

Written by Alex Marin · July 27th, 2023 · 5min read

Working with registry files is quite a simple task. However, depending on the action you want to take, you can encounter various scenarios.

You can either handle your registry directly using VBscript or PowerShell techniques, or use Advanced Installer’s Registry Page to define the needs and integrate them in the installer.

In this article, we’ll go through how to perform operationswith registry files. But before we go further, let's understand what a .reg file is.

What is a .reg file?

A .reg file is a registration file used by the Windows Registry. It contains hives, keys and values. Registry or .reg files can be created from scratch in a text editor like Notepad, or you can manually export them from the Windows Registry. To export a hive or key:

  • Open regedit.exe;
  • Right-click the keys;
  • Select Export.
export registry

Now, let's dive into some of the options for importing registry files.

How to import per-machine registry files?

Microsoft offers the native reg.exe utility for handling operations related to registry files, you can add, delete, export, and import them.

For example, if you want to import a .reg file, all you need to do is open CMD (Command Prompt) as an administrator and type:

Reg import <filename>

If you want to force which registry view to use, you can add the following parameters:

/reg:32    Specifies the key should be accessed using the 32-bit registry view.
/reg:64    Specifies the key should be accessed using the 64-bit registry view.

If you want to import a .reg file with PowerShell, you can use the following code:

$filepath = "C:\Demo.reg"
$proc = start-process reg.exe -ArgumentList "import $filepath" -PassThru -Wait
if ($proc.ExitCode -eq 0) {
	'Success!'
}
else {
	"Fail! Exit code: $($Proc.ExitCode)"
}

To import a .reg file with VBScript, you can use the following code:

Option Explicit
On Error Resume Next
Dim objShell, ProgramFiles, sRegFile
Set objShell = CreateObject(“WScript.Shell”)
sRegFile = "C:\Demo.reg"
objShell.Run “Cmd.exe /K REG.EXE IMPORT ” & Chr(34) &sRegFile & Chr(34),0,True
Set objShell = Nothing

How to import per-user registry files?

If you are planning to work with per-user registry files, you must consider the context in which you will run the actions. Importing on your current logged-in user with a .reg file containing user registry (HKCU) follows the same procedure as with per-machine registry files.

As I said in the MSI Packaging Book, IT Pros typically perform the testing of the installers in “System Context”, with the NT Authority\System account. If you run your commands using the System Context, then the per-user file won’t be imported on all your users or the current logged in user.

An efficient way to deploy per-user .reg files on all users is to use the PowerShell App Deployment Toolkit (PSADT). As mentioned in our book, PSADT offers multiple custom cmdlets.

In this case, we are interested in the Invoke-HKCURegistrySettignsForAllUsers cmdlet. This one is mostly used to specify the registry you want to populate in all the users hives, so you can design the following script:

[scriptblock]$import = {
Execute-Process -Path 'regedit' -Parameters "/q ${dirSupportFiles}\Demo.reg"
}
$AvailableProfiles = Get-Userprofiles -ExcludeNTAccount
Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $import -UserProfiles $AvailableProfiles

We are importing the Demo.reg file inside a scriptblock that runs under the Invoke-HKCURegistrySeetingsForAllUsers cmdlet. Then, we are making sure that we run the action only on the user profiles excluding the NT Authority\System one.

NoteThe .reg file must be placed under the “Files” folder, which is available for PSADT as mentioned in our section Create Scripts in the MSI Packaging Book.

How to import registry files in Advanced Installer?

With Advanced Installer, you can import a .reg file into your MSI with just a few clicks:

  1. Go to the Registry Page.
  2. In the upper menu, you'll find the Import REG button.
  3. Clicking on it reveals three import options:

- Import Key

- Import File

- Import File (Advanced)

When you choose Import Key, Advanced Installer will bring up the registry from your current machine, that allows you to select the key you want to import.

import registry key

Selecting Import File brings up a file selection window, which offers you an opportunity to import your .reg file directly.

import your .reg file directly

For importing multiple .reg files or leveraging advanced options, opt for Import File (Advanced). This choice allows you to specify which components your registry should be mapped onto:

importing multiple .reg files

NoteWe escape using formatted escape characters sequence ([\]) in all formatted special chars (e.g. [, {, etc) so that at install time they will be written in the registry exactly as they are.

It's important to understand how your MSI will be built and the expected behavior concerning the registry. Here is a breakdown:

  1. When you import per-machine registry files, no additional adjustments are required for the package.
  2. On the other hand, importing a per-user registry (HKCU) necessitates understanding of a particular trait:

- During installation, the HKCU will only be placed on the currently logged-in user if the installation is performed under a user with administrative rights.

- Alternatively, the HKCU will be placed on the NT Authority\System when the installation is managed through infrastructure tools like SCCM or Intune.

If you wish to publish the per-user registry on all users, consider the following:

- You can use the Active-Setup mechanism. Remember, this mechanism is not exclusive to MSIs; it's a universal method for all applications you aim to run across all users.

Advanced Installer simplifies the implementation of Active Setup in your package:

- Navigate to the Product Details Page.

- Click on Active Setup.

- Then, select New to generate the Active Setup.

Advanced Installer will take care of the rest of the operations such as deleting the Active Setup keys during uninstall.

deleting the active setup keys

New to Advanced Installer? Explore a more efficient way to handle registry files. Starting your 30-day free trial today!

Video

Conclusion

With these simple steps, you can now seamlessly import registry files. This article offers a comprehensive guide to make your registry files' importation straightforward and efficient. We hope you find it helpful and informative.

Written by
See author's page
Alex Marin

Application Packaging and SCCM Deployments specialist, solutions finder, Technical Writer at Advanced Installer.

Comments: