Skip to content

PowerShell Copying Registry Keys

How to use PowerShell's Copy-Item cmdlet to duplicate registry keys, values, and subkeys (including cross-hive operations like copying from HKLM: to HKCU:) with practical backup and restore examples.

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.

copy-registry-key.ps1powershell
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, ValueCount

where:

  • Copy-Item copies an item or container from one path to another within or across providers.
  • -Path is the source registry key to copy.
  • -Destination is the target key path where the copy will be placed.
  • -Recurse instructs PowerShell to copy all child subkeys and their associated values recursively.
  • -Force overwrites destination keys or properties if they already exist.
  • -PassThru returns the newly created destination Microsoft.Win32.RegistryKey object to the pipeline.
Consolepowershell
./copy-registry-key.ps1
Outputplaintext
Name                                        SubKeyCount ValueCount
----                                        ----------- ----------
HKEY_CURRENT_USER\Software\SourceApp_Backup           0          1

Verifying the Copy

Consolepowershell
Get-ItemProperty -Path "HKCU:\Software\SourceApp_Backup"
Outputplaintext
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\Registry

Copying 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.

Consolepowershell
Copy-Item -Path "HKCU:\Software\MyApp" -Destination "HKCU:\Software\MyApp_Backup" -Recurse

where -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.

copy-cross-hive.ps1powershell
$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 -Verbose

where:

  • $sourcePath is the machine-level configuration path residing in HKEY_LOCAL_MACHINE.
  • $destPath is the user-level configuration path residing in HKEY_CURRENT_USER.
  • Test-Path checks for the parent destination path before initiating the copy.
  • -Verbose outputs line-by-line confirmation of the copy operation across the hives.
Consolepowershell
./copy-cross-hive.ps1
Outputplaintext
VERBOSE: 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.

copy-registry-value.ps1powershell
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-ItemProperty copies a specific named property from one registry key container to another.
  • -Path is the key hosting the source property.
  • -Destination is the key where the property will be copied.
  • -Name is the exact identifier of the property value to copy.
  • Get-ItemPropertyValue reads the copied value directly from the destination key.
Consolepowershell
./copy-registry-value.ps1
Outputplaintext
2.4.0

Practical 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.

backup-and-modify.ps1powershell
$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:

  • $backupKey is 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.
Consolepowershell
./backup-and-modify.ps1
Outputplaintext
Backup created at HKCU:\Software\MyApp_Backup_20240521
MaxRetries updated.

Restoring from a Backup

Consolepowershell
Remove-Item -Path "HKCU:\Software\MyApp" -Recurse -Force
Copy-Item -Path "HKCU:\Software\MyApp_Backup_20240521" -Destination "HKCU:\Software\MyApp" -Recurse

Snapshotting 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.

backup-registry-key.ps1powershell
$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.
  • $backupKey constructs the unique rollback key path using string interpolation.
  • Test-Path ensures the script only attempts to clone existing registry paths.
  • $original and $backup retrieve metadata objects for both the original key and the new snapshot to verify integrity.
Consolepowershell
./backup-registry-key.ps1
Outputplaintext
Snapshotting HKCU:\Software\CorpApp\Settings to HKCU:\Software\CorpApp\Settings_bak_20260929_231026...

TargetKey BackupKey                    ValuesCopied SubkeysCopied
--------- ---------                    ------------ -------------
Settings  Settings_bak_20260929_231026            0             0
Navigation