Create New Registry Keys Using PowerShell

  1. Back up the Registry
  2. Create New Registry Keys Using PowerShell

Sometimes, editing the Windows registry is necessary to rectify errors, modify Windows features, or uninstall software or a product. Even if using the Registry Editor may be convenient, an error could still happen if we are not attentive enough.

Therefore, our local system relies on you to carefully edit the registry.

This article will discuss how to safely add and edit registry key values using Windows PowerShell.

Back up the Registry

It is customary to create a backup of our registry before making any changes, so let’s do that first. As previously said, any unintentional alteration to the registry could result in errors with your files, software, or the entire operating system.

However, you can relax knowing that you can undo any modifications that might cause issues with your operating system if you have a copy of the keys you’re modifying.

We might utilize the antiquated instructions reg export to fast backup your registry. The code shown below will backup the entire HKCU registry.

reg export HKCU C:\Temp\HKCU.Reg /y

We will export the backup of your registry keys as .REG files. Therefore, you may rapidly restore the backed-up registry keys by double-clicking the registry file and following the on-screen instructions.

We may now proceed with editing our registry keys after backing up.

Create New Registry Keys Using PowerShell

For this article, we will discuss two possible ways where we can create new registry entries. First, let’s start with discussing the native PowerShell way, followed by using the .NET class commands.

Create New Registry Keys Using the New-Item Command

When using the New-Item command, we will utilize the pipeline to give names and values to our new registry. We will start with creating the registry inside the registry hive using the New-Item command.

Then, edit the created registry with its name and value using the New-ItemProperty command. The example snippet of code is shown below.

New-Item 'HKCU:\Software\Test' -Force | New-ItemProperty -Name Test -Value Test -Force

Remember when using the -Force parameter, in this case, it will overwrite any existing registry keys contained in the registry hive.

Create New Registry Keys Using the .NET Classes

For our second method, we will take advantage of Microsoft’s Win32 classes. Commonly, it is discouraged to use .NET classes if there is a native PowerShell way of accomplishing a specific use case.

However, for this method, it is more convenient to use this method.

The Microsoft Win32 class uses a registry function called Set-Value(), which accepts three values.

  1. The registry path or hive
  2. Name of the registry
  3. Value of the registry
[Microsoft.Win32.Registry]::SetValue("HKEY_CURRENT_USER\Software\Test", "Test", "Test")

If we compare this method with our previous method, we will notice that everything is processed in just one line, and we do not need to put or use any pipeline methods. Therefore, this method makes it more convenient than our previous method.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - PowerShell Registry

  • Find Users HKEY_USERS Registry Key Using PowerShell
  • Get Registry on a Remote Computer in PowerShell
  • Get the Value of a Registry Key Using PowerShell