Just as Copy-Item duplicates files and folders on the file system, the same cmdlet can duplicate registry keys, copying a key and its values (and optionally subkeys) to a new location within the registry. Because the Registry provider presents hives as navigable drives, you can duplicate entire registry branches, clone configuration templates between hives (such as from HKLM: to HKCU:), and create pre-modification backups directly through the shell.
Duplicating Keys with Copy-Item
The Copy-Item cmdlet copies a registry key (container), its individual values (properties), and optionally all nested subkeys to a new target destination in the registry.
New-Item -Path "HKCU:\Software\SourceApp" -Force | Out-Null
Set-ItemProperty -Path "HKCU:\Software\SourceApp" -Name "AppVersion" -Value "2.4.0"
Copy-Item -Path "HKCU:\Software\SourceApp" `
-Destination "HKCU:\Software\SourceApp_Backup" `
-Recurse `
-Force `
-PassThru | Select-Object -Property Name, SubKeyCount, ValueCountwhere:
Copy-Itemcopies an item or container from one path to another within or across providers.-Pathis the source registry key to copy.-Destinationis the target key path where the copy will be placed.-Recurseinstructs PowerShell to copy all child subkeys and their associated values recursively.-Forceoverwrites destination keys or properties if they already exist.-PassThrureturns the newly created destinationMicrosoft.Win32.RegistryKeyobject to the pipeline.
./copy-registry-key.ps1Name SubKeyCount ValueCount
---- ----------- ----------
HKEY_CURRENT_USER\Software\SourceApp_Backup 0 1Verifying the Copy
Get-ItemProperty -Path "HKCU:\Software\SourceApp_Backup"AppVersion : 2.4.0
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\SourceApp_Backup
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software
PSChildName : SourceApp_Backup
PSDrive : HKCU
PSProvider : Microsoft.PowerShell.Core\RegistryCopying Subkeys with -Recurse
By default, Copy-Item only copies the top-level values of a key: it does not copy subkeys unless -Recurse is specified.
Copy-Item -Path "HKCU:\Software\MyApp" -Destination "HKCU:\Software\MyApp_Backup" -Recursewhere -Recurse copies the entire subtree beneath the source key, including all nested subkeys and their values.
Copying Keys Across Different Hives
Registry keys can be copied across hives, not just within the same one. Because PowerShell abstracts registry hives into standardized PSDrives, Copy-Item allows copying keys across completely different hives, such as replicating machine-wide defaults from HKLM: into a specific user's HKCU: hive.
$sourcePath = "HKLM:\SOFTWARE\Policies\CustomCorp"
$destPath = "HKCU:\Software\Policies\CustomCorp"
if (-not (Test-Path -Path "HKCU:\Software\Policies")) {
New-Item -Path "HKCU:\Software\Policies" -Force | Out-Null
}
Copy-Item -Path $sourcePath -Destination $destPath -Recurse -Force -Verbosewhere:
$sourcePathis the machine-level configuration path residing inHKEY_LOCAL_MACHINE.$destPathis the user-level configuration path residing inHKEY_CURRENT_USER.Test-Pathchecks for the parent destination path before initiating the copy.-Verboseoutputs line-by-line confirmation of the copy operation across the hives.
./copy-cross-hive.ps1VERBOSE: Performing the operation "Copy Item" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\CustomCorp Destination: HKEY_CURRENT_USER\Software\Policies\CustomCorp".Copying Individual Values with Copy-ItemProperty
When you only need to copy a specific configuration value from one existing key to another (rather than copying the entire key container), use the Copy-ItemProperty cmdlet.
New-Item -Path "HKCU:\Software\TargetApp" -Force | Out-Null
Copy-ItemProperty -Path "HKCU:\Software\SourceApp" `
-Destination "HKCU:\Software\TargetApp" `
-Name "AppVersion"
Get-ItemPropertyValue -Path "HKCU:\Software\TargetApp" -Name "AppVersion"where:
Copy-ItemPropertycopies a specific named property from one registry key container to another.-Pathis the key hosting the source property.-Destinationis the key where the property will be copied.-Nameis the exact identifier of the property value to copy.Get-ItemPropertyValuereads the copied value directly from the destination key.
./copy-registry-value.ps12.4.0Practical Usage
Backing Up a Key Before Modification
A common safety pattern is copying a registry key to a _Backup location before making changes, allowing an easy rollback if something goes wrong.
$sourceKey = "HKCU:\Software\MyApp"
$backupKey = "HKCU:\Software\MyApp_Backup_$(Get-Date -Format 'yyyyMMdd')"
if (Test-Path -Path $sourceKey) {
Copy-Item -Path $sourceKey -Destination $backupKey -Recurse
Write-Output "Backup created at $backupKey"
} else {
Write-Output "Source key not found: $sourceKey"
}
Set-ItemProperty -Path $sourceKey -Name "MaxRetries" -Value 5
Write-Output "MaxRetries updated."where:
$backupKeyis a dynamically named backup path that includes the current date, avoiding overwriting previous backups.Get-Date -Format 'yyyyMMdd'formats today's date for use in the backup key name.
./backup-and-modify.ps1Backup created at HKCU:\Software\MyApp_Backup_20240521
MaxRetries updated.Restoring from a Backup
Remove-Item -Path "HKCU:\Software\MyApp" -Recurse -Force
Copy-Item -Path "HKCU:\Software\MyApp_Backup_20240521" -Destination "HKCU:\Software\MyApp" -RecurseSnapshotting a Registry Key Before Updates
Before executing scripts that alter critical application or system keys, administrators should capture an automated snapshot of the target key. This enables rapid rollback if the new configuration causes application failures.
$targetKey = "HKCU:\Software\CorpApp\Settings"
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$backupKey = "${targetKey}_bak_$timestamp"
if (Test-Path -Path $targetKey) {
Write-Output "Snapshotting $targetKey to $backupKey..."
Copy-Item -Path $targetKey -Destination $backupKey -Recurse -Force
$original = Get-Item -Path $targetKey
$backup = Get-Item -Path $backupKey
[PSCustomObject]@{
TargetKey = $original.PSChildName
BackupKey = $backup.PSChildName
ValuesCopied = $backup.ValueCount
SubkeysCopied = $backup.SubKeyCount
}
}
else {
Write-Warning "Target key $targetKey does not exist. Backup skipped."
}where:
Get-Date -Format "yyyyMMdd_HHmmss"generates a timestamp string to ensure unique snapshot key naming.$backupKeyconstructs the unique rollback key path using string interpolation.Test-Pathensures the script only attempts to clone existing registry paths.$originaland$backupretrieve metadata objects for both the original key and the new snapshot to verify integrity.
./backup-registry-key.ps1Snapshotting HKCU:\Software\CorpApp\Settings to HKCU:\Software\CorpApp\Settings_bak_20260929_231026...
TargetKey BackupKey ValuesCopied SubkeysCopied
--------- --------- ------------ -------------
Settings Settings_bak_20260929_231026 0 0