Introduction:

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.

The 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

PowerShell output to NULL

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

PowerShell output to NULL

Ways to send console output to NULL:

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.
    PowerShell output to NULL
  2. Typecasting to [void] : This is more of a very C#-flavored trick

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

    PowerShell output to NULL
  4. Assignment to $NULL: Assigning the output to the Automatic variable $NullPowerShell output to NULL

NOTE: The performance varies 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.

performance of sending PowerShell output to NULL

If you like this article read more articles similar articles under ‘N – Ways to’ category

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

signature