Suppress console output in PowerShell
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
.
Comments
-
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 almost 11 years
>$null
does the same as| Out-Null
. -
Ansgar Wiechers almost 11 yearsIn PowerShell v3 he could redirect all output streams like this:
*>$null
. -
Erutan409 almost 9 yearsMaybe so..but it would make more sense to use this cmdlet, instead of remembering an arbitrary "hack" to cancel out the output.
-
theyetiman over 8 years@Erutan409 why is
>$null
a hack? Serious question. -
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 over 7 yearsDefinitely a matter of opinion,
>$null
is easily guessable for me as linux user -
Kody over 7 yearsSee this post: stackoverflow.com/questions/5260125/…
-
Ben Personick over 7 yearsI 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 over 7 yearsHmm, 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 almost 7 years@AnsgarWiechers Perhaps add that as an answer, so it can receive upvotes.
-
Ansgar Wiechers almost 7 years@DanSolovay Nah. It's just a minor variation of Dave's answer.
-
JustAGuy almost 6 yearsThis is definitely the right answer. | out-null was just not enough.
-
spicy.dll almost 6 yearsWhy don't you flag as duplicate then?
-
Dirk almost 6 yearsBecause I've never done it and was uncertain about it. Now it is flagged.
-
Fütemire over 2 yearsI'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.