How to migrate registry entries with PowerShell with Get-Item : Set-Item and ExportTo-CliXml

2.6k views Asked by At

I am trying to export selective registry entries, preferences and configurations for various software applications, and them import them on to a new machine.

Get-ChildItem Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\OneNote -Recurse | Export-Clixml -Depth 3 -Path OneNote.xml

And then reverse this process on another machine at a different time using

Import-CliXml -Path OneNote.xml | Set-Item

This doesn't seem to work but it's not obvious why. Conceptually it feels as though something like this should work.

Ideally the next step would be to be able to get keys, filter them and store them all in one file and then restore them cleanly on another machine.

This can be done with regedit, but I'm curious as to how this is done with PowerShell.

Rather than creating the keys if they don't exist and setting their values the Import-CliXml command creates values under default property names in the keys that correspond to the paths of the key. For example:

On the destination machine, a target key such as:

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\OneNote\OpenNoteBooks

will now contain a default item which has the value:

"HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\OneNote\OpenNoteBooks" 

rather than the expected properties from the source key.

For context, at a basic level, what I wish to do may be accomplished by:

reg export HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\OneNote OneNote.reg 

To create a registry file containing the keys an values, then.

reg import OneNote.reg 

On the target machine to load them. Obviously this is easy for a simple "dump and restore" operation, however it is less flexible if one wishes to process they keys using some logic first.

1

There are 1 answers

0
Kev On BEST ANSWER

There's a couple of reasons why this won't work as expected.

Firstly in the Set-Item documentation notes section it states:

In the Registry drives, HKLM: and HKCU:, Set-Item changes the data in the (Default) value of a registry key. To create and change the names of registry keys, use New-Item and Rename-Item. To change the names and data in registry values, use New-ItemProperty, Set-ItemProperty, and Rename-ItemProperty.

So right off the mark we're destined for this not working as you'd intuitively think.

Secondly if you look at the output from Import-CliXml -Path OneNote.xml, there's no registry value type information provided e.g. DWORD, QWORD etc, therefore any tool using this as input won't know how to construct a key's values properly because there's no type hinting.

You're better off sticking to the good old reg.exe tool. It's well proven and predictable in its behaviour. Also it's not too hard to parse a .reg file if you need to tweak some values along the way.