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 keys, and 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 sets 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 the 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.

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 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 the creation of 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, registry 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