PowerShell export registry key

2 minute read

Window registry has been for many years the joy and pain of every Windows Systems Administrator and if memory does not fail me it was even part of some of the old NT4 exams (the sweet sting of nostalgia!).

MCSE Windows NT

Anyhow as I said at some point or another we all had to deal with the import/export/modification of a registry key on a Windows machine hence this post.

I am developing an automation solution to maintain an internal Windows CA Server and part of the requirement is exporting some registry keys.

Method 1 - Regexport

Windows has a built-in utility called regexport which was designed for the purpose of exporting registry key and can easily be called from within PowerShell with something like this

Invoke-Command  {reg export 'HKLM\SOFTWARE\Microsoft\Office\ClickToRun\Configuration' C:\Temp\MyKeyNackup.reg

While this works generally speaking I am not a big fan of calling external executables from within PowerShell for a number of reasons, chief among all the pain to handle return values from the external exe.

Method 2 - Export-Registry PowerShell function

Being confronted with this specific requirement/challenge I decided to come up with something native. Result is the Export-Registry function which allows export of key both in XML and CSV format. I’ve added the latter as I find it handy for reporting purposes.

You can find the full function code in my GitHub function repository but here’s couple of examples.

# Define registry keys to export
[array]$registryKey = @('HKCU:\SOFTWARE\Ditto\',

# Export keys and remove any binary data
Export-Registry -KeyPath $registryKey -ExportFormat xml -ExportPath 'C:\Temp\TestExport.xml' -NoBinaryData

The above will check if the specified key exist via the Test-IsRegistryKey helper function and once validated export the key in XML format.

Here’s the sample export

Objs Version="" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <Obj N="Path" RefId="1">
        <TN RefId="1">
          <I32 N="SubKeyCount">1</I32>
          <Obj N="View" RefId="2">
            <TN RefId="2">

I have truncated the output for brevity but all the data in the registry will be converted and written to the specified XML file.

Here is an example of the same command just using the CSV export format


Note: In order to for the Export-Registry function to work you will also need to import/download the Test-IsRegistryKey one as well.

While format is not the native .reg of regexport with this approach we can have a much easier and better error and exception handling.

Hopefully you will find this functions as useful as they’ve been for me!

Final thoughts

If you’re wondering yes, I am that old to own and have used that book to prepare for the NT4 MCSE Exam. I even have somewhere a book completely dedicated to NT4 registry.

Relics of an era when you could not simply google for an answer.