Suppress console output in PowerShell

227,005

Solution 1

Try redirecting the output like this:

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose >$null 2>&1

Solution 2

Try redirecting the output to Out-Null. Like so,

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose | out-null

Solution 3

It is a duplicate of this question, with an answer that contains a time measurement of the different methods.

Conclusion: Use [void] or > $null.

Share:
227,005
Dominik Antal
Author by

Dominik Antal

Forever learning.

Updated on July 05, 2022

Comments

  • Dominik Antal
    Dominik Antal almost 2 years

    I have a call to GPG in the following way in a PowerShell script:

    $key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose > $null
    

    I don't want any output from GPG to be seen on the main console when I'm running the script.

    Due to my noobness in PowerShell, I don't know how to do this. I searched Stack Overflow and googled for a way to do it, found a lot of ways to do it, but non of it worked.

    The "> $null" for example has no effect. I found the --quiet --no-verbose options for GPG to put less output in the console, still it's not completely quiet, and I'm sure there is a way in PowerShell too.

  • Ansgar Wiechers
    Ansgar Wiechers almost 11 years
    >$null does the same as | Out-Null.
  • Ansgar Wiechers
    Ansgar Wiechers almost 11 years
    In PowerShell v3 he could redirect all output streams like this: *>$null.
  • Erutan409
    Erutan409 almost 9 years
    Maybe so..but it would make more sense to use this cmdlet, instead of remembering an arbitrary "hack" to cancel out the output.
  • theyetiman
    theyetiman over 8 years
    @Erutan409 why is >$null a hack? Serious question.
  • Erutan409
    Erutan409 over 8 years
    @theyetiman Matter of opinion, I suppose. Using PowerShell's built-in Out-Null, I think, would read better when debugging someone else's code or even your own if it's been a while. It's an intentional function that's provided for the aforementioned problem, too.
  • codaamok
    codaamok over 7 years
    Definitely a matter of opinion, >$null is easily guessable for me as linux user
  • Kody
    Kody over 7 years
  • Ben Personick
    Ben Personick over 7 years
    I hadn't thought about it much but I had assumed that " > $null" was an alias of " | Out-Null" to allow us CMD and Bash users a quick 'hack' to use it. However on thinking about it " | Out-Null" does use a pipe, and therefore will need at least one extra execution step over >..... Might be that " | Out-Null" is to make a standardized usage within powershell,a nd ultimately over-rides " > $null" which would make " > $null" a preferable usage. Looks like I may have to change my scripts currently using " | Out-Null" to use " > $null" instead.
  • Ben Personick
    Ben Personick over 7 years
    Hmm, just did a little googling, and this Stack Exchange Item, seems to show that " > $null" is quite a bit faster at allowing command execution. stackoverflow.com/questions/5260125/…
  • Dan Solovay
    Dan Solovay almost 7 years
    @AnsgarWiechers Perhaps add that as an answer, so it can receive upvotes.
  • Ansgar Wiechers
    Ansgar Wiechers almost 7 years
    @DanSolovay Nah. It's just a minor variation of Dave's answer.
  • JustAGuy
    JustAGuy almost 6 years
    This is definitely the right answer. | out-null was just not enough.
  • spicy.dll
    spicy.dll almost 6 years
    Why don't you flag as duplicate then?
  • Dirk
    Dirk almost 6 years
    Because I've never done it and was uncertain about it. Now it is flagged.
  • Fütemire
    Fütemire over 2 years
    I'd be careful with this option, if you are trying to suppress console output but also trying to return results, this will actually return null and not your results.