powershellregistryremote-registry

Powershell throws terminating error on reg import but operation completes successfully


Both this stack post as well as this one looked similar but I couldn't find my solution there. I'm having an issue understanding the difference between the behaviour in reg export versus reg import.

When running reg export inside powershell, it returns "operation completed successfully" but reg import throws a terminating error saying "operation completed successfully". The registry file is correctly imported even though it is thrown as an error. An example:

PS C:\Windows\System32> reg export HKLM\Software\MySoftware C:\Scripts\MyFile.reg
The operation completed successfully.

PS C:\Windows\System32> reg import C:\Scripts\MyFile.reg
reg : The operation completed successfully.
At line:1 char:1
+ reg import C:\Scripts\MyFile.reg
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (The operation completed successfully.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Microsoft's documentation of the reg import command says that the return codes are 0 for Success and 1 for Failure. Why would Powershell return the success as a regular success message for reg export, but throw it as a terminating error for reg import?

I'm running Powershell as an Admin and the logged in user is a local administrator on the server. Any assistance is appreciated.


Solution

  • Your "problem" is, you are using PowerShell in ISE. Try using PowerShell directly. The difference? Two strange things first:

    1. reg export writes its output on success to stdout. reg import writes its output on success to stderr (I would consider that as "strange").
    2. ISE throws an exception, if an external program writes to stderr. PowerShell does not do that.

    In conclusion, ISE throws an error on an actual success message, because it has been written to stderr. You can prevent that, using the Start-Process cmdlet:

    Start-Process reg -ArgumentList "import C:\Scripts\MyFile.reg"
    

    Because stdout and stderr of the external program are then not forwarded to your terminal. Instead, you can access them through the parameters -RedirectStandardOutput and -RedirectStandardError, if needed.

    If you want to evaluate the exit code, you can do it like this:

    $process = Start-Process reg -ArgumentList "import C:\Scripts\MyFile.reg" -PassThru -Wait
    $process.ExitCode