89

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dominik Antal
  • 3,281
  • 4
  • 34
  • 50
  • 1
    See also ... http://stackoverflow.com/questions/16744451/powershell-suppress-output-from-non-powershell-commands – SteveC May 29 '14 at 14:59
  • 1
    possible duplicate of [How to suppress stderr output in Powershell?](http://stackoverflow.com/questions/11969596/how-to-suppress-stderr-output-in-powershell) – nobody Jun 20 '14 at 18:27
  • 2
    Possible duplicate of *[What's the better (cleaner) way to ignore output in PowerShell?](https://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in-powershell)*. – Peter Mortensen Oct 21 '17 at 12:07

3 Answers3

146

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

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose | out-null
KyleMit
  • 30,350
  • 66
  • 462
  • 664
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • 4
    `>$null` does the same as `| Out-Null`. – Ansgar Wiechers Sep 13 '13 at 09:51
  • 16
    Maybe so..but it would make more sense to use this cmdlet, instead of remembering an arbitrary "hack" to cancel out the output. – Erutan409 Sep 17 '15 at 18:39
  • @Erutan409 why is `>$null` a hack? Serious question. – theyetiman Feb 02 '16 at 12:50
  • 2
    @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. – Erutan409 Feb 02 '16 at 12:57
  • Definitely a matter of opinion, `>$null` is easily guessable for me as linux user – codaamok Nov 03 '16 at 11:04
  • See this post: http://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in-powershell – Kody Nov 14 '16 at 17:10
  • 1
    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 Nov 16 '16 at 15:00
  • 2
    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. http://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in-powershell – Ben Personick Nov 16 '16 at 15:03
  • 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. – Fütemire Mar 25 '22 at 14:56
54

Try redirecting the output like this:

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose >$null 2>&1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56
8

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

Conclusion: Use [void] or > $null.

Community
  • 1
  • 1
Dirk
  • 1,134
  • 2
  • 12
  • 21