93

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.

3

3 Answers 3

152

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

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose | out-null
Sign up to request clarification or add additional context in comments.

10 Comments

>$null does the same as | Out-Null.
Maybe so..but it would make more sense to use this cmdlet, instead of remembering an arbitrary "hack" to cancel out the output.
>$null is hardly an arbitrary hack; it is very familiar to those coming from other shell scripting languages. Using the cmdlet is more idiomatic to powershell, though.
@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.
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.
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/…
|
57

Try redirecting the output like this:

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

5 Comments

In PowerShell v3 he could redirect all output streams like this: *>$null.
@AnsgarWiechers Perhaps add that as an answer, so it can receive upvotes.
@DanSolovay Nah. It's just a minor variation of Dave's answer.
This is definitely the right answer. | out-null was just not enough.
this - same as the above answer - does not assign the value to the variable - it just sinks everything into $null
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.

2 Comments

Why don't you flag as duplicate then?
Because I've never done it and was uncertain about it. Now it is flagged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.