Import script-modified .reg files with PowerShell?

23,637

Solution 1

As far as I know, basic PowerShell Cmdlets don't use .reg file at all.

So, on one hand you can modify your reg files and apply them with REG.EXE. On the other hand you can use PowerShell registry provider an 'Item' CMdlets to modify your registry.

Solution 2

You could create reg commands in memory and execute those without resorting to temp files. That is, construct reg add commands like so,

reg add \\reg-path /v value-name /f /t type-name /d value-data

which will add or overwrite value value-name with value-data as data into registry path reg-path.

Creating such commands in a Powershell loop shouldn't be too hard.

Share:
23,637
Hydrargyrum
Author by

Hydrargyrum

Updated on November 07, 2020

Comments

  • Hydrargyrum
    Hydrargyrum over 3 years

    I need to import a .reg file that contains standard configuration settings for a server product. However, it's possible that a single Windows box might contain multiple server instances. Each server instance has its own configuration keys in the Windows registry.

    The first instance will have the default registry key:

    HKLM\SOFTWARE\<Vendor>\<Product>\Settings
    

    Any other instances will have variant names, eg:

    HKLM\SOFTWARE\<Vendor>\<Product>\Settings-foo
    HKLM\SOFTWARE\<Vendor>\<Product>\Settings-bar
    HKLM\SOFTWARE\<Vendor>\<Product>\Settings-baz
    

    etc...

    The registry file contains settings that need to be applied to each of these server instances. The structure of the registry sub-keys is the same within each key. So, the manual deployment process is to take the .reg file and do a file search-and-replace for "SOFTWARE\<Vendor>\<Product>\Settings\" and replace it with "SOFTWARE\<Vendor>\<Product>\Settings-foo\", then import the newly tweaked .reg file. Rinse & repeat for bar, baz etc.

    What I want to do is write a PowerShell script that gets a list of all the "Settings-" keys and does the equivalent search-and-replace before importing the .reg file. I haven't found a cmdlet that can import .reg files, so I guess I have to call reg.exe or regedit.exe. Both of those programs allow you to import the contents of a .reg file that's on disk.

    My question is, do I actually have to create .reg files and write them to disk so that I can then call reg.exe /Import <filename> or regedit.exe /S <filename>? Or is there some way I can just load the original .reg file, modify it in memory, and get reg.exe or regedit.exe to import the modified registry keys without needing to write a whole stack of .reg files to disk?