reg import returns (The operation completed successfully.:String) [], RemoteExce

Working with registry in PowerShell. I export a key using reg export for backup and that works fine and as expected, but when I run reg import to restore from the backup, it returns as an error although it is successful.

I saw this post: https://powershell.org/forums/topic/reg-exe-throws-an-error-on-success-in-remote-session/

But in my case, this is not on a remote machine and I do not want to suppress errors, but capture and handle them.

The key I export and import is in the HKEY_CLASSES_ROOT hive.

The import does work(!), just returns an exception as a result for some reason.

I can do as suggested in the post above and then check $lastexitcode but prefer to do something more elegant.

Any ideas?

PS V5.1 but requires compatibility to V4

Hmmm … that’s actually not Powershell at all. reg.exe is a windows command line tool.
Anyway without seeing your actual code and the exact and complete error message we can barely guess what’s wrong.

Yes, I am aware that reg.exe is a windows cmd tool, but it is PowerShell that, for some reason, interprets success as an error

All I do is something like this:

reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\v3.5 c:\test11.reg
reg import c:\test.reg

When I run it in cmd, it works fine, the output is “The operation completed successfully.” on both commands

However, in PowerShell, the first commands yields the same result: “The operation completed successfully.”

But the second results in this:

reg : The operation completed successfully.
At line:1 char:1
+ reg import c:\test.reg
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The operation completed successfully.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Now, when I try to assign these to a variable, like:

$regexport = reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\v3.5 c:\test11.reg
$regimport = reg import c:\test.reg

The value of $regexport will be “The operation completed successfully.”

But the value of $regimport will be “”, although it is successful. I need to know the import was successful.

So yes, I can check $lastexitcode, but the behavior seems very strange

The “exception” is only happening in ISE, but I don’t get the value also in the console

Obviously reg import has a bug. The success message appears on the error channel. You can catch this when you use Start-Process with the parameter -RedirectStandardError.

Start-Process -FilePath reg -ArgumentList 'import c:\test.reg' -NoNewWindow -Wait -RedirectStandardError C:\reg-error.log
Get-Content -Path C:\reg-error.log
Remove-Item -Path C:\reg-error.log

That works

Thanks!

Yu can do like below with native executables.

$regimport = reg import c:\test.reg 2>&1
$regimport