Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress console output in PowerShell

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.

like image 500
Dominik Antal Avatar asked Sep 13 '13 07:09

Dominik Antal


People also ask

How do I run a PowerShell script silently?

You can either run it like this (but this shows a window for a while): PowerShell.exe -WindowStyle hidden { your script.. } Or you use a helper file I created to avoid the window called PsRun.exe that does exactly that. You can download the source and exe file from Run scheduled tasks with WinForm GUI in PowerShell.

What does out-null do in PowerShell?

The Out-Null cmdlet sends its output to NULL, in effect, removing it from the pipeline and preventing the output to be displayed at the screen.

What does $_ in PowerShell mean?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

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

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

like image 197
vonPryz Avatar answered Sep 21 '22 15:09

vonPryz


Try redirecting the output like this:

$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose >$null 2>&1 
like image 26
Dave Sexton Avatar answered Sep 19 '22 15:09

Dave Sexton