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! 😉
My new book : PowerShell Scripting Guide to Python
https://leanpub.com/PowerShell-to-Python/embed
This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.
“Use what you know to learn what you don’t. ” also known as Associative learning.
Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –
- Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
- Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.
Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloudto the future – Data Science, Artificial Intelligence (AI) and Machine Learning Python is a must know.
But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell
NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!
While the book is in progress, please review it and send any feedback or error corrections at prateek@ridicurious.com
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. 😉
LikeLike
I like topics linked with performance PowerShell. Thanks
LikeLiked by 1 person
[…] have also tried the same using the blog to see the logs if its appear or not using pipeline with | Out-Null and can able to not getting any […]
LikeLike
[…] intentamos lo mismo usando el Blog para ver los registros if su apariencia o no usar tubería con | Out-Null y es posible que no […]
LikeLike