Last active
April 29, 2023 12:52
-
-
Save klevcsoo/f26e03aa491a48be27f6517014a17d01 to your computer and use it in GitHub Desktop.
PowerShell registry search-replace script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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
$RegistryPath = "-------"
$SearchQuery = "-----"
$NewValue = "-----"
)
$Registry = Get-ChildItem $RegistryPath -Recurse
foreach ($a in $Registry) {
$a.GetValue($ ) -Like "$SearchQuery"
$a.Property | Where-Object {
} | 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
}
}