How to Create a Registry Key and Values using Powershell

My previous blog post discussed deleting a registry key and values using PowerShell. This is helpful if you want to delete a registry entry under a registry key. It also includes an additional script that 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 keys and entries and add values using PowerShell. You can create any registry entry under a registry key, such as a String, DWORD, Binary, etc. You can also create multiple registry entries with different data sets.

For example, I will create a registry key called cloudinfra.net and three registry entries called Download, Location, and Status, with values of 123 (decimal), United Kingdom, and 1, respectively. The screenshot below shows the registry keys and 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, such as a registry entry with values, which I will discuss at the end of this blog post.

Create registry keys and Values using Powershell

Since I’ve already provided an example registry key and its entries that we’ll be creating today, I’ll proceed directly to the script and provide details about the variables and data types used.

A few points about the script:

  • $regpath – Provide a registry key path for 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 and its data type.
  • It will check to see if the registry key exists and then create the registry key and registry entries.

Powershell Script | 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 several alternative methods for creating or updating registry keys and entries. Please find them listed below:

1. Using the 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’ve explored how to create registry keys and entries and assign values using PowerShell. Additionally, we’ve discussed alternative methods for working with registry entries.

If you need to verify the existence of a specific registry key, you can refer to my other blog post titled Powershell to Test If Registry Key and Value Exist For guidance on deleting registry keys and entries using PowerShell, you can follow the instructions in the blog post titled How to Delete a Registry Entry Using PowerShell.

Leave a Comment