Suppress output from non-PowerShell commands?
Solution 1
Out-Null
works just fine with non-PowerShell commands. However, it doesn't suppress output on STDERR
, only on STDOUT
. If you want to suppress output on STDERR
as well you have to redirect that file descriptor to STDOUT
before piping the output into Out-Null
:
hg st 2>&1 | Out-Null
2>
redirects all output from STDERR
(file descriptor #2). &1
merges the redirected output with the output from STDOUT
(file descriptor #1). The combined output is then printed to STDOUT
from where the pipe can feed it into STDIN
of the next command in the pipline (in this case Out-Null
). See Get-Help about_Redirection
for further information.
Solution 2
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.
ping -n 2 $APP 2>&1 | Write-Verbose
Solution 3
Can also do this
hg st *> $null
Powershell suppress console output
![Abhishek](https://i.stack.imgur.com/u41IF.jpg?s=256&g=1)
Abhishek
I have been a senior developer with a focus on architecture, simplicity, and building effective teams for over ten years. As a director at Surge consulting I was involved in many operational duties and decisions and - in addition to software development duties - designed and implemented an interview processes and was involved in community building that saw it grow from 20 to about 350 developers and through an acquisition. I was then CTO setting up a dev shop at working closely with graduates of a coding bootcamp on both project work and helping them establish careers in the industry. Currently a Director of Engineering at findhelp.org your search engine for finding social services. I speak at conferences, have mentored dozens of software devs, have written popular articles, and been interviewed for a variety of podcasts and publications. I suppose that makes me an industry leader. I'm particularly interesting in companies that allow remote work and can check one or more of the following boxes: Product companies that help people in a non-trite manner (eg I'm not super interested in the next greatest way to get food delivered) Product companies that make developer or productivity tooling Funded startups that need a technical co-founder Functional programming (especially Clojure or Elixir) Companies trying to do something interesting with WebAssembly
Updated on May 26, 2020Comments
-
Abhishek about 4 years
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. -
Abhishek about 11 yearsAwesome. That works. Can you explain what the
2>&1
syntax actually means? -
Abhishek about 10 yearsThis won't suppress STDERR for example if
hg
is not installed -
Bill_Stewart about 10 years
2>&1
means "redirect standard error to the same place as standard output." -
Ansgar Wiechers about 10 years@Bill_Stewart I believe that's what I said, only that I addressed both parts of the operator separately, because other streams could be merged with STDOUT (the success output stream, actually) as well.
-
Bill_Stewart about 10 years@AnsgarWiechers - sure. I didn't notice that you had amended your answer after George Mauer asked for clarification.
-
Ansgar Wiechers over 9 years@GeorgeMauer Neither does my suggestion. Errors thrown by the host environment are not redirected.
-
Ansgar Wiechers over 9 yearsNote that the
*>
redirection operator is not available prior to PowerShell v3.