Skip to content

Instantly share code, notes, and snippets.

@klevcsoo
Last active April 29, 2023 12:52
Show Gist options
  • Save klevcsoo/f26e03aa491a48be27f6517014a17d01 to your computer and use it in GitHub Desktop.
Save klevcsoo/f26e03aa491a48be27f6517014a17d01 to your computer and use it in GitHub Desktop.
PowerShell registry search-replace script
Param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $RegistryPath,
[Parameter(Mandatory = $true, Position = 1)]
[string] $SearchQuery,
[Parameter(Mandatory = $true, Position = 2)]
[string] $NewValue
)
$Registry = Get-ChildItem $RegistryPath -Recurse
foreach ($a in $Registry) {
$a.Property | Where-Object {
$a.GetValue($_) -Like "*$SearchQuery*"
} | ForEach-Object {
$CurrentValue = $a.GetValue($_)
$ReplacedValue = $CurrentValue.Replace($SearchQuery, $NewValue)
Write-Output "Changing value of $a\$_ from '$CurrentValue' to '$ReplacedValue'"
Set-ItemProperty -Path Registry::$a -Name $_ -Value $ReplacedValue
}
}
@FrightRisk
Copy link

Hello. Do you have any instructions on how to use this? I want to search the entire registry for "mark" and replace it with "fred". Can you help?

@mrTigerFox
Copy link

$RegistryPath = "-------"
$SearchQuery = "-----"
$NewValue = "-----"
)

$Registry = Get-ChildItem $RegistryPath -Recurse

foreach ($a in $Registry) {
$a.Property | Where-Object {
$a.GetValue($) -Like "$SearchQuery"
} | ForEach-Object {
$CurrentValue = $a.GetValue($
)
$ReplacedValue = $CurrentValue.Replace($SearchQuery, $NewValue)
Write-Output "Changing value of $a$_ from '$CurrentValue' to '$ReplacedValue'"
Set-ItemProperty -Path Registry::$a -Name $_ -Value $ReplacedValue
}
}

@MikedaSpike
Copy link

Thanks for this !
Was searching to do a replace of values c:<Dir> to d:<Dir> in my registry and this example really helped !
I should only change line 17 to
$ReplacedValue = $CurrentValue.tolower().Replace($SearchQuery.ToLower(), $NewValue)
This will ignore the casing of the search

And responding to @FrightRisk
Save the file and run it like this :
.\replacereg.ps1 "HKCU:" "C:\Pinball" "D:\Pinball"
This replaces all my c:\pinball entries to d:\pinball

Again thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment