Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suppress standard error output in PowerShell?

In a PowerShell script automating some SVN tasks I have the following function:

function SvnUrlExists($url) {   svn info $url | out-null 2>&1   return $? } 

Since this explicitly tests whether some SVN repository URL exists, I am not interested at all in any error output. However, despite everything I found about redirecting stderr in PowerShell suggesting 2>&1 to redirect it to stdout, this still outputs an error message:

 svn: warning: W170000: URL 'blahblah' non-existent in revision 26762 svn: E200009: Could not display info for all targets because some targets don't exist 

Unfortunately, this severely messes up the output of my script.

What am I doing wrong, and how should I suppress this error output?

like image 262
sbi Avatar asked Aug 15 '12 12:08

sbi


People also ask

How do you ignore an error in PowerShell and let it continue?

If there are special commands you want to ignore you can use -erroraction 'silentlycontinue' which will basically ignore all error messages generated by that command. You can also use the Ignore value (in PowerShell 3+): Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.

How do you clear error variables in PowerShell?

What to do if you want to clean out all the entries in $error? $error is a variable, so you can try with the Clear-Variable cmdlet: PS> Clear-Variable error -Force Clear-Variable : Cannot overwrite variable Error because it is read-only or constant.

How do you catch error messages in PowerShell?

To catch the error you will need toadd the -ErrorAction Stop parameter behind your action. Another option is to change the ErrorActionPreference at the beginning of your script or PowerShell session. But keep in mind that the preference will reset to continue when you start a new session.


1 Answers

If you want to suppress only standard error, use:

svn info $url 2>$null 
like image 123
Daniel Avatar answered Sep 21 '22 20:09

Daniel