I am running a command
hg st
and then checking it's $LASTEXITCODE
to check for availability of mercurial in the current directory. I do not care about its output and do not want to show it to my users.
How do I suppress ALL output, success or error?
Since mercurial isn't a PowerShell commandlet hg st | Out-Null
does not work.
Can also do this
Powershell suppress console output
A fun thing you can do is to pipe the output to Write-Verbose, then you can still see it if you need it by running your script with the -Verbose switch.
Out-Null
works just fine with non-PowerShell commands. However, it doesn't suppress output onSTDERR
, only onSTDOUT
. If you want to suppress output onSTDERR
as well you have to redirect that file descriptor toSTDOUT
before piping the output intoOut-Null
:2>
redirects all output fromSTDERR
(file descriptor #2).&1
merges the redirected output with the output fromSTDOUT
(file descriptor #1). The combined output is then printed toSTDOUT
from where the pipe can feed it intoSTDIN
of the next command in the pipline (in this caseOut-Null
). SeeGet-Help about_Redirection
for further information.