Error when calling 3rd party executable from Powershell when using an IDE
Another way to suppress the NativeCommandError
output is to convert the objects in the pipeline to strings as outlined at the bottom of this answer:
du c:\Backup 2>&1 | %{ "$_" }
To avoid this you can redirect stderr to null e.g.:
du 2> $null
Essentially the console host and ISE (as well as remoting) treat the stderr stream differently. On the console host it was important for PowerShell to support applications like edit.com to work along with other applications that write colored output and errors to the screen. If the I/O stream is not redirected on console host, PowerShell gives the native EXE a console handle to write to directly. This bypasses PowerShell so PowerShell can't see that there are errors written so it can't report the error via $error or by writing to PowerShell's stderr stream.
ISE and remoting don't need to support this scenario so they do see the errors on stderr and subsequently write the error and update $error.
I've recently been facing the same issues, but I would like to get the stderr output directed to stdout. You would think that the following would work:
& du 2>&1
But PowerShell will interpret the redirection and process it after 'du' is completed. The work-around I found is to invoke it using cmd.exe /c:
& cmd /c 'du 2>&1'