PowerShell provides a built-in cmdlet New-Item to create a registry key and a New-ItemProperty cmdlet to add a registry key value.
The following methods can be used to create a registry key in PowerShell
Method 1: Creating a registry key in PowerShell
New-Item -Path $registryPath -Name "AppKey"
In this example, the New-Item cmdlet uses the -Path parameter to specify the registry path where you want to create a registry key “AppKey“.
Method 2: Adding registry key value in PowerShell
New-ItemProperty -Path $registryPath -Name "SysKey" -Value "0100-0fc00-0bd2-002a" -PropertyType String
In this example, the New-ItemProperty cmdlet adds a registry key value to “SysKey” having a property type string.
The following example shows how to use these methods.
How to Create a Registry Key in PowerShell
To create a registry key in PowerShell, you can use the New-Item cmdlet along with the -Path and -Name parameters.
The following example shows how to do it with syntax.
# Specify the registry path $registryPath = "HKLM:\Software\SysApp" # Create the new registry key New-Item -Path $registryPath -Name "AppKey" # Retrieve the registry key details Get-Item $registryPath
Output:
In this PowerShell script, the $registryPath variable holds the path to the registry where you want to create a new registry key “AppKey“.
The New-Item cmdlet uses the -Path parameter to specify the registry path $registryPath and the -Name parameter specifies the new name for the registry key “AppKey“.
Finally, after running the script, a new registry key named “AppKey” will be created at the specified path.
You can retrieve the newly created registry key details using the Get-Item cmdlet.
Adding a Registry Key Value in PowerShell
To add a registry key and its value in PowerShell, use the New-Item cmdlet to create a registry key in combination with the New-ItemProperty cmdlet to add a registry value within the newly created registry key.
The following example shows how to do it with syntax.
# Specify the registry path $registryPath = "HKLM:\Software\SysApp" # Create the new registry key New-Item -Path $registryPath -Name "AppKey" # Add a registry key value New-ItemProperty -Path $registryPath -Name "SysKey" -Value "0100-0fc00-0bd2-002a" -PropertyType String
Output:
In this PowerShell script, the New-ItemProperty cmdlet uses the -Path parameter to specify the registry path where we want to create a new registry key “SysKey” using the New-Item cmdlet.
The New-ItemProperty cmdlet creates a new value named “0100-0fc00-0bd2-002a” within the “SysKey” registry key. The -Value parameter sets the value data to “0100-0fc00-0bd2-002a“, and the -PropertyType parameter specifies that the value type is a string type.
Conclusion
I hope the above article on creating a registry key in PowerShell using the New-Item cmdlet is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.