Powershell Export Multiple Keys To One .reg File

18,512

The simplest way would be to export each key individually, and then merge the resulting files:

$keys = 'HKLM\Software\Test\ABC', 'HKLM\Software\ABC\123', ...

$tempFolder = 'C:\temp\folder'
$outputFile = 'C:\path\to\merged.reg'

$keys | % {
  $i++
  & reg export $_ "$tempFolder\$i.reg"
}

'Windows Registry Editor Version 5.00' | Set-Content $outputFile
Get-Content "$tempFolder\*.reg" | ? {
  $_ -ne 'Windows Registry Editor Version 5.00'
} | Add-Content $outputFile
Share:
18,512
Eric
Author by

Eric

Working on build/release management and deployment using nANT/MSBUILD, CruiseControl.net, Team Foundation Server, Powershell, Jenkins build server, Atlassian tool stack (Bamboo, BitBucket, JIRA, etc.)

Updated on June 04, 2022

Comments

  • Eric
    Eric almost 2 years

    I would like to export multiple registry keys to the same .reg file. Every suggestion I've seen shows to use reg /e [key name] filename.reg, but I have a list of 4-5 registry entries I want to export and doing it this way will overwrite it each time. What I want is something like:

    • Export HKLM\Software\Test\ABC RegFile.reg
    • Export HKLM\Software\ABC\123 RegFile.reg
    • Export HKLM\Software\XYZ\Lala RegFile.reg

    So that each registry key is appended to the same .reg file, not overwritten each time. How can I do this?

  • Eric
    Eric over 9 years
    Thank you for the reply. Using this example creates the "temp" files (1,2,3.reg, etc), but the merged file just contains the "Windows Registry Editor" header. I have checked my paths and everything looks good. Maybe it's not liking the Get-Content command? Any ideas? Thanks in advance!
  • Eric
    Eric over 9 years
    After looking at things further, it seems that I am getting "cannot access the file merged.reg because it is being used by another process." Do I need to release control of the file in between each Add-Content call?
  • Eric
    Eric over 9 years
    EDIT: After adding parenthesis around the (Get-Content) command the script works perfectly. Apparently, adding parenthesis forces the script to retrieve and store all files in memory at once, instead of having to access it each time it loops. Thanks again for your help!
  • Ansgar Wiechers
    Ansgar Wiechers over 9 years
    @Eric If you place the output file in the same folder as the input file you need to make sure you exclude that file from being processed by Get-Content. Or read everything completely before proceeding to writing the output file, as you have.
  • Grhm
    Grhm almost 6 years
    If you want the resulting .reg file to work with reg.exe, you may need to add an -Encoding Unicode parameter to the Set-Content and Add-Content calls. I believe that since Powershell 6.0, the default encoding is UTF-8 (see github.com/PowerShell/PowerShell/issues/4878) and .reg files are expected to be UTF16-LE which is Powershell's 'Unicode' encoding.
  • Ansgar Wiechers
    Ansgar Wiechers almost 6 years
    @Grhm The default encoding for the *-Content cmdlets used to be "Ascii", which would be perfectly fine for .reg files. Has that actually changed in PowerShell v6?
  • Grhm
    Grhm almost 6 years
    @AnsgarWiechers On a Windows Server 2016 box, I had to add the -Encoding Unicode to make & reg.exe import $outputFile work. Without specifying an encoding it failed with a message saying something like 'not a valid registry file'. I was exporting HKCU\Control Panel\International which has non-ansi characters in some of the key names.
  • Grhm
    Grhm almost 6 years
    @AnsgarWiechers Just checked - Server2016 (with PSVersion 5.1.14393.2312) if I don't specify encoding I get an ANSI-encoding file. But reg.exe exports as UTF16LE-with-BOM. It'll accept ANSI - but if you've got anything that contains extended characters in your registry, it will be converted to "????". I hit this as my reg file included the key [HKEY_CURRENT_USER\Control Panel\International\🌎🌏🌍] with the 3 globe characters!!
  • Ansgar Wiechers
    Ansgar Wiechers almost 6 years
    Yeah, that's basically the behavior I would have expected. Thanks for verifying.
  • tukan
    tukan over 2 years
    This way you would have multiple 'Windows Registry Editor Version 5.00' at your output...