Suppress output from non-PowerShell commands?

前端 未结 3 1711
梦如初夏
梦如初夏 2021-02-05 02:27

I am running a command

hg st

and then checking it\'s $LASTEXITCODE to check for availability of mercurial in the current director

相关标签:
3条回答
  • 2021-02-05 03:05

    Can also do this

    hg st *> $null
    

    Powershell suppress console output

    0 讨论(0)
  • 2021-02-05 03:06

    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
    
    0 讨论(0)
  • 2021-02-05 03:15

    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.

    0 讨论(0)
提交回复
热议问题