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.
Your "problem" is, you are using PowerShell in ISE. Try using PowerShell directly. The difference? Two strange things first:
reg exportwrites its output on success tostdout.reg importwrites its output on success tostderr(I would consider that as "strange").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 theStart-Processcmdlet:Because
stdoutandstderrof the external program are then not forwarded to your terminal. Instead, you can access them through the parameters-RedirectStandardOutputand-RedirectStandardError, if needed.If you want to evaluate the exit code, you can do it like this: