Tell PowerShell ISE not to send stderr to Write-Error

The PowerShell console and PowerShell ISE behave differently when executables are written to the standard error stream ( stderr ). The console (powershell.exe) displays it as standard output. For example, when I get status with Mercurial in a non-repository, it writes a standard error:

 > hg st -RC:\Windows abort: repository C:\Windows not found! 

However, in PowerShell ISE ( powershell_ise.exe ), this error is passed to the PowerShell Write-Error cmdlet:

 > hg st -RC:\Windows hg.exe : abort: repository C:\Windows not found! At line:1 char:3 + hg <<<< st -RC:\Windows + CategoryInfo : NotSpecified: (abort: repository C:\Windows not found!:String) [], RemoteExcepti on + FullyQualifiedErrorId : NativeCommandError 

Is there a way to configure ISE to behave like a console and not send the stderr stream to Write-Error ?

+8
powershell powershell-ise
source share
2 answers
 hg st -RC:\Windows 2>&1 | %{ if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.Exception.Message } else { $_ } } 

This saves the output of stderr and sends it as normal output, rather than discarding it.

+1
source share

Redirecting stderr output to stdout "should" work, but it is not in ISE. In this case, it is best to disable the error output as follows:

 & { $ErrorActionPreference = 'SilentlyContinue' hg st -RC:\Windows 2>&1 } 

When you configure this variable in a nested scope, you do not set it globally. When the above region exits, the global version of $ErrorActionPreference is still set to what it was before.

Unfortunately, ISE and the console behave differently, but I understand that using the "console" another console application simply receives a console descriptor, so it is output directly to the console (bypassing PowerShell). ISE is not a console, so it tries to get the native stderr to play well with the PowerShell error stream. IMO console behavior in this case is not ideal. So, is ISE better to be compatible with the console, or is it better to have the stderr ISE descriptor better (with the exception of the bit to prevent thread redirection)? Obviously, PowerShell went with the latter.

+7
source share

All Articles