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
or, loading assemblies in the PowerShell script like in the following screenshot which outputs the success indication once the assembly has been loaded.
Following are 4 methods to send output to null and suppress these outputs in the console –
- Using Out-Null Cmdlet : Hides/discards the output instead of sending it down the pipeline or displaying it.
- Typecasting to [void] : This is more of a very C#-flavored trick
- Redirection to $NULL: Redirecting the output to the Automatic variable $Null
- Assignment to $NULL: Assigning the output to the Automatic variable $Null
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.
Please do follow me on twitter and thanks for reading. Cheers! 😉