Search and replace registry entry value using powershell

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

Related videos on Youtube

jessehouwing
Author by

jessehouwing

Updated on September 18, 2022

Comments

  • jessehouwing
    jessehouwing almost 2 years

    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.