How to set a Windows registry value with PowerShell

Written by Alex Marin · November 23rd, 2023 · 5min read

Customizations for applications inside the Windows OS are usually done either in files or the registry, depending on the developer's choice.

Usually, developers choose to place the settings inside the registry because it’s much easier to manage and manipulate. It also makes things easier when it comes to deployments inside infrastructures.

The Windows registry can be edited easily outside of the package with the many scripting languages and programming languages out there, which makes it quite useful.

In this article, we will explore setting up Windows registry values with native PowerShell commands but also with the popular PowerShell framework that is designed for IT Professionals, called PowerShell App Deployment Toolkit (PSADT).

What is Windows Registry?

Think of the Windows registry as the “behind the scenes” control center. It is basically a structured “database” where Windows and other programs can store configuration settings. These settings are stored inside keys with multiple types of values.

From a programmatic standpoint, there are many alternatives to editing the registry. However back in the day everything basically relied on "reg.exe". The "reg.exe" was introduced with Windows NT and its purpose was to enable a simpler programmatic way to manipulate the registry. Of course, another way of editing the registry is via the WMI (Windows Management Instrumentation).

reg.exe command

How to Set a Registry Key with PowerShell

This might be complex information to process when we discuss editing some registry keys.

Let's see how PowerShell operates: inside the PowerShell console there are multiple “providers” that act as an intermediary layer. Inside these providers you basically have a range of cmdlets that you can use to manipulate the system.

In PowerShell, Microsoft added the Registry provider. This allows you to use registry-related cmdlets to manipulate the registry according to your needs. A Get-PSProvider cmdlet that is executed in the PowerShell console will display all the providers that are available on your machine.

Get-PSProvider in Windows PowerShell

The above providers are standard on each machine that runs on Windows. As a short explanation:

  • Registry: As mentioned above, this provider deals with the Windows Registry and offers access to the keys available on the machine
  • Alias, Environment and Function: These are providers which offer access to aliases, environment variables and functions
  • FileSystem: I think it’s quite explanatory that this one provides access to manage files and folders

When working with Registry (or any other type of provider), these create virtual drivers, which make it easier to access parts of the registry. For example, in our Registry provider when we say HKLM: or HKCU:, we are basically “creating drives” that we access for the registry hive.

Setting registry values

There are two scenarios that we need to keep in mind when we talk about registry manipulation with PowerShell, and we need to understand when to use New-ItemProperty or Set-ItemProperty.

NoteFor a more in-depth understanding of PowerShell concepts, we recommend you get and read the free e-book PowerShell for Beginners.

For example, let’s imagine that we want to create a “Version” registry value under "HKCU:\Software\Test\MyKey" with a value of "12".

$RegistryPath = 'HKCU:\Software\Test\MyKey’
$Name         = 'Version'
$Value        = ‘12’
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

However, if the key doesn’t exist and we use the New-ItemProperty cmdlet, we will get an error:

New-ItemProperty : Cannot find path 'HKCU:\Software\Test\MyKey' because it does not exist.
New-Item property error

How do you address this issue?

Well, it is recommended whenever working with the registry and PowerShell to test every possible scenario. In this case, we have to check if the key exists. If it doesn't, we must create it using the "New-Item" cmdlet.

# Set variables to indicate value and key to set
$RegistryPath = 'HKCU:\Software\Test\MyKey’
$Name         = 'Version'
$Value        = '12'
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
  New-Item -Path $RegistryPath -Force | Out-Null
}  
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force
New-Item to create the registry key

If we know for sure that the registry key value already exists, then we can use the Set-ItemProperty cmdlet. Check out this example:

# Define the registry path and property name
$RegistryPath = 'HKCU:\Software\Test\MyKey’
$PropertyName = 'Version'
# Specify the new value
$NewValue = '13'
# Use Set-ItemProperty to update the registry value
Set-ItemProperty -Path $RegistryPath -Name $PropertyName -Value $NewValue
Set-ItemProperty to update the registry value

ImportantTinkering with the registry, whether through the Registry Editor or PowerShell commands, can have serious consequences if done improperly.
Always exercise caution, back up critical data, and be certain of the changes you're making. The power of the registry comes with the responsibility to use it wisely!

Set a registry key with PowerShell App Deployment Toolkit

As you might have seen, working with registry keys is not that trivial with classical PowerShell cmdlets, and many verifications must be made in order for the registry alterations to be done accordingly.

However, in the IT Professionals workspace, a new popular tool emerged, and this is the PowerShell App Deployment Toolkit, or PSADT for short.

This is merely a framework with its own custom cmdlets that can be used, which makes IT Professionals' lives much easier when it comes to device administration.

When we think about registry manipulation, we can consider the custom Set-RegistryKey function available in PSADT.

Basically, all the checks that you’ve seen up top don’t have to be done, because the Set-RegistryKey function is doing all the work in the background.

# Set a registry value to configure application behavior
Set-RegistryKey -Key 'HKLM\Software\YourApplication' -Name 'SettingName' -Value 'DesiredValue'
# Create a new registry key if it doesn't exist
Set-RegistryKey -Key 'HKLM\Software\YourApplication\NewKey'
# Modify an existing registry value
Set-RegistryKey -Key 'HKCU\Software\YourApplication' -Name 'ExistingSetting' -Value 'UpdatedValue'

Conclusion

We've seen how useful the "Set-RegistryKey" cmdlet in the PSADT framework can be. It can be one of your best tricks in software deployment. It's like having a magic tool for the Windows Registry, making sure that your software runs seamlessly on every computer.

Happy scripting!

Written by
See author's page
Alex Marin

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

Comments: