How to create a registry Key and values using Powershell

In my previous blog post, I discussed about how to delete a registry key and values using Powershell. This post is helpful if you want to delete a registry entry that exists under a registry key. There is an additional script in this blog post, which also checks for the data type of a registry entry before it is deleted.

In this blog post, I will show you different ways to create registry key, registry entries and add values using PowerShell. You can create any type of registry entry under a registry key, for example: String, DWORD, Binary etc. Also you can create multiple registry entries with different data set as well.

For example, I will be creating a registry key called cloudinfra.net along with three registry entries called Download, Location, and Status with their values 123 (decimal), United Kingdom, and 1 respectively. As you can see a screenshot below shows the registry keys and registry entries after running the Powershell script.

Registry keys and Registry entries with values created using Powershell script
Registry keys and Registry entries with values created using Powershell script

I will be using New-Item PowerShell cmdlet and Set-ItemProperty Powershell cmdlet to create these values. There are other ways to create a registry key, registry entry with values which I will discuss at the end of this blog post.

Steps to create registry keys and entries using Powershell

As I have already provided an example registry key and entries which I will be creating today, I will directly jump onto the script and provide details about variables and data type used.

A few points about the script:

  • $regpath – Provide registry key path in this variable. for example: HKLM:\Software\cloudinfra.net.
  • $regValues – Provide all the registry entries and their values you want to create under the cloudinfra.net registry key along with its data type.
  • It will check to see if the registry key exists or not and then proceed with creation of registry key and registry entries.

CreateregistryItems.ps1

<#
.DESCRIPTION
    This script will create a registry key cloudinfra.net along with 
    three registry entries called Location, Download and Status
    Author: Jatin Makhija
    Website: Copyright - Cloudinfra.net
    Version: 1.0.0
#>
# Define the registry key path
$regPath = "HKLM:\Software\cloudinfra.net"
# Create an array of registry values with their data and data types
$regValues = @{
    "Location" = @{
        Data = "United Kingdom"
        Type = "String"
    }
    "Download" = @{
        Data = 123
        Type = "DWord"
    }
    "Status" = @{
        Data = "1"
        Type = "String"
    }
}
# Create the registry key if it doesn't exist
if (-not (Test-Path -Path $regPath)) {
    New-Item -Path $regPath -Force
    Write-Host "Registry key $regPath created."
}
# Create or update the registry values
foreach ($name in $regValues.Keys) {
    $value = $regValues[$name]
    $data = $value.Data
    $type = $value.Type
    # Set the registry value with the specified data type
    Set-ItemProperty -Path $regPath -Name $name -Value $data -Type $type
    Write-Host "Registry value $name set in $regPath with data $data and type $type."
}
Powershell script execution result for creating registry keys
Powershell script execution result for creating registry keys

Alternative ways to create registry keys using Powershell

There are few alternative ways using which you can create or update registry keys and registry entries. Please find it below:

1. Using reg add command

Add a registry entry using reg add command

reg add "HKLM\Software\cloudinfra.net" /v Currency /d "GBP" /f
Add a registry entry using reg add command
Add a registry entry using reg add command

Update a registry entry using reg add command

reg add "HKLM\Software\cloudinfra.net" /v Location /d "NewYork" /f
Update a registry entry using reg add command
Update a registry entry using reg add command

2. Using New-Itemproperty cmdlet

You can use New-Itemproperty powershell cmdlet to create or update registry entries in a registry key.

Create a new registry entry using New-Itemproperty cmdlet

New-ItemProperty -Path HKLM:\Software\cloudinfra.net -Name "Pincode" -Value 1234567890
Create a new registry entry using New-Itemproperty
Create a new registry entry using New-Itemproperty

Conclusion

In this blog post, we have seen how to create registry keys, registry entries and assign their values using PowerShell. We have also seen alternative ways to create and update registry entries.

If you want to test the existence of a particular registry key then you can follow my other blog post: Powershell to test If registry key and value exists. For deleting a registry key and entries using Powershell, you can follow blog post: How to delete a registry entry using Powershell.

READ NEXT