4 ways to send output to NULL in PowerShell


Often you will be in situations when you want to perform an operation at the same time suppress any output sent to the PowerShell console.

Best use case could be when creating a new directory an output is sent to the console with the name, size, time and other details. In some scripting scenarios you won’t be willing to see this output on the console and send it to NULL.

Like, creating a temporary file/directory during the script execution which may mess up the desired/custom script output on the console

1

or, loading assemblies in the PowerShell script like in the following screenshot which outputs the success indication once the assembly has been loaded.

2017-10-07_1-27-52

Following are 4 methods to send output to null and suppress these outputs in the console –

  1. Using Out-Null Cmdlet : Hides/discards the output instead of sending it down the pipeline or displaying it.

    1

  2. Typecasting to [void] : This is more of a very C#-flavored trick

    1
  3. Redirection to $NULL:  Redirecting the output to the Automatic variable $Null

    1
  4. Assignment to $NULL: Assigning the output to the Automatic variable $Null

    1

NOTE: The performance vary in all 4 approaches, in the following screenshot you can clearly see the difference. Using Out-Null is the slowest but, the usage of Automatic variable $NULL is faster compared to other approaches.

1

Please do follow me on twitter and thanks for reading. Cheers! 😉

signature

Advertisements

2 thoughts on “4 ways to send output to NULL in PowerShell

  1. So using the old DOS redirection > is way faster then using the shinny new PS pipeline to Out-Null cmdlet. Who’d a thunk it. 😉

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s