Kidbuu
Kidbuu

Reputation: 69

Powershell Registry Search & Edit

I need a script that can find a particular default value among multiple registry keys and change it.

Here is what I can do so far:

Get-ChildItem -path HKCU:\SOFTWARE\Classes\CLSID\

The CLSID key contains lots of key subkeys such as {021E4F06-9DCC-49AD-88CF-ECC2DA314C8A}.

I need to find the subkey that contains the default value of "My company name" and then make a change to it.

The problem is that the name of the target key is not fixed and varies across machines.

For instance, on my machine it is:

HKEY_CURRENT_USER\Software\Classes\CLSID\{04271989-C4D2-51FA-6558-1FD935F1416C}

One another machine it is:

HKEY_CURRENT_USER\Software\Classes\CLSID\{04271989-C4D2-9527-CD60-A32EA8C49FE9}

How can I get this done?

Any help will be appreciated

Upvotes: 1

Views: 1206

Answers (2)

mklement0
mklement0

Reputation: 439832

PowerShell's registry provider implementation is notoriously hard to work with, but you can use the following approach:

  • Use the registry provider via Get-ChildItem to get all subkeys of a given key.

    • Get-ChildItem and Get-Item work well for returning and enumerating registry keys, optionally via wildcard expressions.

    • Things get problematic when trying to work with the values of the keys returned, due to the quirks of the Get-ItemProperty and Get-ItemPropertyValue cmdlets; the next point shows how to avoid them.

  • Then make direct use of the Microsoft.Win32.RegistryKey instances that are returned (instead of using cmdlets).

    • Use .GetValue('') / .GetValue($valueName) to return the unnamed (default) / the given value's data.

    • Use .SetValue($valueName, $data) to update existing values (again use '' to update the unnamed (default) value).

    • Use .DeleteValue($valueName) to delete a existing value.

    • Use .CreateSubKey($key) to create a new subkey.

    • Use .DeleteSubKey($key) / .DeleteSubKeyTree($key) to delete a subkey / delete it recursively.

    • Use .GetValueNames() to enumerate all value names, and .GetSubKeyNames() to enumerate all subkey names.

Note: The variables such as $valueName and $data used above are just placeholders for whatever real values you need, irrespective of whether you specify them as literals (e.g., 0, 'yes') or via your own variables (e.g., $foo = 'new'; $_.SetValue('', $foo)

Applied to your scenario:

Get-ChildItem HKCU:\SOFTWARE\Classes\CLSID | # Loop over subkeys of ...\CLSID
  where { $_.GetValue('') -eq 'My company name'} | # Match against default value
   foreach {
     # Modify the key; e.g. with $_.SetValue($valueName, $data)
   }

Upvotes: 1

Joost
Joost

Reputation: 1216

This is something to get you started. It lists all item properties of each GUID in HKCU, where the default value matches a partial string.

Get-ChildItem "HKCU:\Software\Classes\clsid" | 
    ForEach-Object { Get-ItemProperty -Path $_.PSPath } | 
    Where-Object {$_.'(default)' -like "Java Plug-in*"}

You can change the '(default)' to 'your company name' to make it work in your scenario.

Upvotes: 0

Related Questions