Create a Registry Keys and Values using PowerShell

In this blog post, I will show you different ways to create registry keys, registry entries and values using PowerShell. You can create any type of 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. Below screenshot shows 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 and Set-ItemProperty PowerShell cmdlets to create these values. There are more ways to create a registry key and add values, I have discussed those options at the end of this blog post.

Create Registry Keys and Values using PowerShell

Let’s 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.

  • A few points about the script:
    • $regpath: Provide a registry key path for this variable. For example, HKLM:\Software\cloudinfra.net.
    • $regValues: Provide the registry entries and their values you want to create under the cloudinfra.net registry key, along with their data type.
    • Script will check to confirm if the registry key exists or not. If it’s not found, then only it will create the 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

Alternative Ways to Create Registry Entries using PowerShell

There are several alternative methods for creating or updating registry 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

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

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

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 want to confirm 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 Delete a Registry Entry Using PowerShell.

Leave a Comment