0

In a script, I have a number of reg import operations. These are all successful, but at the end of my script, I have $Error > C:\Out\Errors.txt that helps to clean up various errors, but I am always left with 4 or 5 outputs like this in the $Error output:

reg : The operation completed successfully.
At D:\MySandbox\MySandbox.ps1:1321 char:18
+ ... e-Command { reg import "$env:TEMP\RegistryEditorSettings.reg" *>&1 |  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (The operation completed successfully.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

I run the reg import operations like this (as a result of reading a Stack Overflow that I can't find now that said you have to do it this way to avoid certain problems - but I'm beginning to think that this syntax is the problem):

Invoke-Command { reg import "$env:TEMP\RegistryEditorSettings.reg" *>&1 | Out-Null }

What would be a recommended way to run reg import operations from my script in such a way that they don't generate output in my $Error (unless there was an error with the reg import)?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • 1
    Up until recent PowerShell version, redirected native commands polluted the `$Error` variable and caused script-terminating error when `$ErrorActionPreference = 'stop'` was in effect (see [details](https://stackoverflow.com/a/59376457/7571258)). If you can't avoid redirection, temporarily set `ErrorActionPreference='Continue'` and remove the unwanted `$Error` records after calling `reg` (e. g. by saving the `$Error.Count` before calling `reg` and delete the difference number of records). Do actual error handling by checking `$LASTEXITCODE`. – zett42 Nov 07 '22 at 17:20
  • 1
    What about `reg import "$env:TEMP\RegistryEditorSettings.reg" > nul 2> nul` directly. It should suppress all the errors OR `reg import "$env:TEMP\RegistryEditorSettings.reg" >nul 2>&1` – Ranadip Dutta Nov 07 '22 at 17:38
  • I'm trying both of these options, thanks, all appreciated. I've just stopped all redirection and it's going ok as the script is run by a `Start-Process -WindowStyle Hidden` process, and I see no errors in my error output now. – YorSubs Nov 08 '22 at 20:37

0 Answers0