Windows – Search and replace registry entry value using powershell

command linefind and replacepowershellwindows-registry

I'm trying to update a set of registry keys, a set of properties need to be updated with a new value based on the old value.

I tried using the following:

 Get-ItemProperty -Path HKCU:\Software\xxxxx\*.mydomain.com Uri 
      | set-itemproperty -Path { $_.PSPath } Uri -Value { $_.Value -Replace ".mydomain.com/", ".mynewdomain.com/" }

But that sets the value of the uri property to: { $_.Value -Replace ".mydomain.com/", ".mynewdomain.com/" }

I tried:

 Get-ItemProperty -Path HKCU:\Software\xxxxx\*.mydomain.com Uri 
      | set-itemproperty -Path { $_.PSPath } Uri -Value ${ $_.Value -Replace ".mydomain.com/", ".mynewdomain.com/" }

And

 Get-ItemProperty -Path HKCU:\Software\xxxxx\*.mydomain.com Uri 
      | set-itemproperty -Path { $_.PSPath } Uri -Value ( $_.Value -Replace ".mydomain.com/", ".mynewdomain.com/" )

But that clears the value.

I want to update multiple registry values in multiple keys with as few lines as possible. I already got it working by exporting the registry, use notepad to search and replace and then re-import the registry entries, but that felt like cheating. I really want to know how I can achieve this using Powershell.

Other things I've tried: $(...), (...), omitting the -Value option you name it :S.

I tried replacing $_.Value with $_.Uri and $_, didn't work either.

Best Answer

 Get-ItemProperty -Path HKCU:\Software\xxxxx\*.mydomain.com Uri | %{set-itemproperty -Path $_.PSPath Uri -Value ( $_.Uri -Replace ".mydomain.com/", ".mynewdomain.com/" )}
Related Question