How to Manage Registry Values with PowerShell
PowerShell exposes registry keys as items and their named values as properties. This distinction replaces much of the old StdRegProv boilerplate. Create the key with New-Item, then manage its values with the property cmdlets.
Use an isolated current-user key
Run this example in PowerShell on Windows. It creates a unique key under your own profile and removes only that key afterward.
$demoKey = 'HKCU:\Software\RegistryDemo-' + [guid]::NewGuid().ToString('N')
try {
New-Item -Path $demoKey -ErrorAction Stop | Out-Null
New-ItemProperty -LiteralPath $demoKey -Name 'Caption' -PropertyType String -Value 'Example' -ErrorAction Stop | Out-Null
New-ItemProperty -LiteralPath $demoKey -Name 'RetryCount' -PropertyType DWord -Value 3 -ErrorAction Stop | Out-Null
Set-ItemProperty -LiteralPath $demoKey -Name 'RetryCount' -Value 5 -ErrorAction Stop
$settings = Get-ItemProperty -LiteralPath $demoKey -ErrorAction Stop
'{0}: {1}' -f $settings.Caption, $settings.RetryCount
Remove-ItemProperty -LiteralPath $demoKey -Name 'Caption' -ErrorAction Stop
(Get-Item -LiteralPath $demoKey).GetValueKind('RetryCount')
} finally {
if (Test-Path -LiteralPath $demoKey) {
Remove-Item -LiteralPath $demoKey -ErrorAction Stop
}
}
The expected output is Example: 5, followed by DWord. The demo key should no longer exist after the final cleanup.
Preserve type and scope
Choose String, ExpandString, DWord or another supported property type deliberately when creating a value. A numeric-looking string is not interchangeable with a DWORD. Read existing values and types before changing an application's configuration.
HKCU means the account running the process, which can be different for a scheduled task or service. On 64-bit Windows, 32-bit and 64-bit processes can also see different redirected registry views. Match the consuming application's account and architecture.
Keep -ErrorAction Stop where failure must abort the operation. A permission error is not evidence that a key is missing. For real settings, preserve the previous value and its existence separately so rollback can restore a value or remove a newly introduced one correctly.
References


Author: Herman Schoenfeld