Video Tutorial:
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
or, loading assemblies in the PowerShell script like in the following screenshot which outputs the success indication once the assembly has been loaded.
Ways to send console output to NULL:
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 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.
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! 😉
Prateek Singh
Related posts
7 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,132,735 People Reached
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. 😉
I like topics linked with performance PowerShell. Thanks
The [void] pattern is best demonstrated with a [void] (command) example
The parenthesis wrapping is needed for examples like
[void] (mkdir mynewestdir)
Thanks for the input, I’ll update the post accordingly.
[…] 4 ways to send output to NULL in PowerShell […]
[…] 4 ways to send output to NULL in PowerShell […]
[…] 4 ways to send output to NULL in PowerShell […]