Search code examples
powershellpowershell-5.0

How to change the data of registry property data without knowing the key beforehand


I'm trying to write code that will find and change the data inside of a registry keys properties if it includes a specific string like 'placeholder'.


Solution

  • I am addressing the question, how do I search property values in the registry? The way I do it is with this script:

    # get-itemproperty2.ps1
    # get-childitem skips top level key properties, use get-item for that
    param([parameter(ValueFromPipeline)]$key)
    
    process { 
      $valuenames = $key.getvaluenames() 
    
      if ($valuenames) { 
        $valuenames | foreach {
          $value = $_
          [pscustomobject] @{
            Path = $key -replace 'HKEY_CURRENT_USER',
              'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
            Name = $Value
            Value = $Key.GetValue($Value)
            Type = $Key.GetValueKind($Value)
          }
        }
      } else {
        [pscustomobject] @{
          Path = $key -replace 'HKEY_CURRENT_USER',
            'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
            Name = ''
            Value = ''
            Type = ''
        }
      }
    }
    

    With that script in hand, here's an example.

    ls -r hkcu:\key1 | get-itemproperty2 | where value -match value
    
    Path            Name  Value    Type
    ----            ----  -----    ----
    HKCU:\key1\key2 name2 value2 String
    

    The results can be used with set-itemproperty.

    ls -r hkcu:\key1 | get-itemproperty2 | where value -match value | 
      set-itemproperty -value myvalue -whatif