Suppress console output in PowerShell
Asked Answered
S

3

91

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.

Sierrasiesser answered 13/9, 2013 at 7:46 Comment(3)
See also ... #16744951Thousandth
possible duplicate of How to suppress stderr output in Powershell?Ashanti
Possible duplicate of What's the better (cleaner) way to ignore output in PowerShell?.Gospel
C
55

Try redirecting the output like this:

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose >$null 2>&1
Colo answered 13/9, 2013 at 9:20 Comment(5)
In PowerShell v3 he could redirect all output streams like this: *>$null.Swainson
@AnsgarWiechers Perhaps add that as an answer, so it can receive upvotes.Neckwear
@DanSolovay Nah. It's just a minor variation of Dave's answer.Swainson
This is definitely the right answer. | out-null was just not enough.Cutout
this - same as the above answer - does not assign the value to the variable - it just sinks everything into $nullNewsman
F
150

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

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose | out-null
Fossa answered 13/9, 2013 at 9:12 Comment(9)
>$null does the same as | Out-Null.Swainson
Maybe so..but it would make more sense to use this cmdlet, instead of remembering an arbitrary "hack" to cancel out the output.Shippy
@Shippy why is >$null a hack? Serious question.Papeterie
@Papeterie 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.Shippy
Definitely a matter of opinion, >$null is easily guessable for me as linux userModest
See this post: #5260625Bashaw
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.Lower
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. #5260625Lower
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.Lynellelynett
C
55

Try redirecting the output like this:

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose >$null 2>&1
Colo answered 13/9, 2013 at 9:20 Comment(5)
In PowerShell v3 he could redirect all output streams like this: *>$null.Swainson
@AnsgarWiechers Perhaps add that as an answer, so it can receive upvotes.Neckwear
@DanSolovay Nah. It's just a minor variation of Dave's answer.Swainson
This is definitely the right answer. | out-null was just not enough.Cutout
this - same as the above answer - does not assign the value to the variable - it just sinks everything into $nullNewsman
H
9

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

Conclusion: Use [void] or > $null.

Heathenism answered 6/12, 2016 at 11:5 Comment(2)
Why don't you flag as duplicate then?Tricornered
Because I've never done it and was uncertain about it. Now it is flagged.Heathenism

© 2022 - 2024 — McMap. All rights reserved.