Skip to content

Instantly share code, notes, and snippets.

@ion1
Created July 30, 2022 22:00
Show Gist options
  • Save ion1/4a28219089002cad018feea760d6213b to your computer and use it in GitHub Desktop.
Save ion1/4a28219089002cad018feea760d6213b to your computer and use it in GitHub Desktop.
Registry search and replace
$Search = "E:\Music Production\"
$Replace = "D:\Music Production\"
function Confirm {
for (;;) {
Write-Host "[YNQ] " -NoNewline
$Key = [System.Console]::ReadKey().Key
Write-Host ""
switch ($Key) {
"Y" { return $true }
"N" { return $false }
"Q" { exit }
}
Write-Host "Please press Y/N/Q"
}
}
Get-ChildItem HKLM: -Recurse -ErrorAction SilentlyContinue | Foreach-Object {
$Path = $_
$Path.Property | ForEach-Object {
$Key = $_
if ($Key -like "*$Search*") {
$NewKey = $Key.Replace($Search, $Replace)
Write-Output "", "$Path", " k- $Key", " k+ $NewKey"
if (Confirm) {
Write-Output "Renaming"
Rename-ItemProperty -Path Registry::$Path -Name $Key -NewName $NewKey
$Key = $NewKey
}
}
$Value = $Path.GetValue($Key)
if ($Value -like "*$Search*") {
$NewValue = $Value.Replace($Search, $Replace)
Write-Output "", "$Path\$_", " - $Value", " + $NewValue"
if (Confirm) {
Write-Output "Changing"
Set-ItemProperty -Path Registry::$Path -Name $Key -Value $NewValue
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment