Exporting keys in .reg file using Powershell

5,640

OK, limited PS skills and you want to automate messing with the registry.

Uhhhhh… are you sure? 8-}

All that being said.

What you show here is fine with the exception of that you don't show the values to be set nor the command to set the registry key.

These cmdlets are the ones you can use to deal with the registry.

Get-Command -CommandType Cmdlet -Name '*item*'


CommandType     Name                  ModuleName
-----------     ----                  ----------
Cmdlet          Clear-Item            Microsoft.PowerShell.Management
Cmdlet          Clear-ItemProperty    Microsoft.PowerShell.Management
Cmdlet          Copy-Item             Microsoft.PowerShell.Management
Cmdlet          Copy-ItemProperty     Microsoft.PowerShell.Management
Cmdlet          Get-ChildItem         Microsoft.PowerShell.Management
Cmdlet          Get-Item              Microsoft.PowerShell.Management
Cmdlet          Get-ItemProperty      Microsoft.PowerShell.Management
Cmdlet          Move-Item             Microsoft.PowerShell.Management
Cmdlet          Move-ItemProperty     Microsoft.PowerShell.Management
Cmdlet          New-Item              Microsoft.PowerShell.Management
Cmdlet          Remove-Item           Microsoft.PowerShell.Management
Cmdlet          Remove-ItemProperty   Microsoft.PowerShell.Management
Cmdlet          Set-Item              Microsoft.PowerShell.Management
Cmdlet          Set-ItemProperty      Microsoft.PowerShell.Management

Be sure to look at the help files and their examples before use as these also.

https://docs.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/working-with-registry-entries?view=powershell-6

https://blogs.technet.microsoft.com/heyscriptingguy/2015/04/02/update-or-add-registry-key-value-with-powershell

PSRemoteRegistry 1.0.0.0

This module contains functions to create, modify or delete registry subkeys and values on local or remote computers.

https://www.powershellgallery.com/packages/PSRemoteRegistry/1.0.0.0

https://stackoverflow.com/questions/28076128/powershell-export-multiple-keys-to-one-reg-file

As we know messing with the registry can really hurt if you are not careful. So, back it up first so you can restore if disaster happens or at least to a system restore point, VM checkpoint/snapshot.

So, here's slight modification to your posted code, but don't take this as final as you need to make decisions on what actions need to be taken and how.

Get-ChildItem "Registry::HKCR" -Recurse -Force `
| where { $_.Name -match 'vlc.'}`
| ForEach-Object {
    try {
            'Target key to modify / export / whatever'
            $_.Name
            # 'Registry code here' -WhatIf # remove the whatif if you are sure you are good with what you have
    }
    catch { 
               Write-Warning -Message 'Key not accessible' 
               $_.Name
          }
}
Share:
5,640

Related videos on Youtube

juju
Author by

juju

“ You take the old Goethe much too seriously, my young friend. ” Der Steppenwolf, Hermann Hesse. Curious & aviation enthusiast. Latécoère 28-1 Forgotten aviation heroes Guillaumet, Almonacid, Mermoz, Saint Ex, for the new Aéropostale line in Latin America

Updated on September 18, 2022

Comments

  • juju
    juju over 1 year

    I need to change several key values in Windows 7 registry, to add a parameter to a command launching VLC.

    Fortunately all keys are children of keys starting with VLC.:

    enter image description here

    The command for Open and PlayWithVLC must be edited. I'm thinking about:

    • exporting keys in a .reg file,
    • editing values externally to add --no-playlist-enqueue to the line
    • re-importing the .reg file in the registry.

    My skills in PowerShell are limited, I assume the code should be something like this:

    Get-ChildItem "Registry::HKCR" -Recurse -Force 
    | where { $_.Name -match 'vlc.'}`
    | ForEach-Object {
        try {
            <create .reg entry>
        }
        catch { }
    }
    

    but I'm stuck at this point. Could you give me some piece of advice on how to proceed further?

    • Next-Door Tech
      Next-Door Tech over 5 years
      The image you've linked is not working. Could you please update it?