Video Tutorial
In order to write less code or to write better one-liners in Powershell, I often find one of these PowerShell Variable assignment techniques useful, and let me know if you feel the same.
-
Using
-OutVariable
parameterGet-Process a* -OutVariable process
-
PowerShell Variable squeezing
Its a shame many of us ( including myself 😉 ) were not aware of this for quite long.
Windows PowerShell supports a simplified syntax for showing the contents of variables during the variable assignment. To do this, wrap the variable assignment statement in parentheses.($process = Get-Process a*)
-
Using Tee-Object Cmdlet
Tee-Object Cmdlet T’s results to o/p stream and Variable $process at the same time, you can use it like in the image belowOr, something like
Get-Process a* | Tee-Object -Variable p
You can use one of the above methods to get the work done and at the same time results stored in a variable with PowerShell one-liner, like in the example below
If you like this article read similar articles under ‘N – Ways to’ category
Please do follow me on twitter and thanks for reading. Cheers! 😉
My New Book : PowerShell Scripting Guide to Python
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
Prateek Singh
Related posts
6 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,541 People Reached
[…] on June 29, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
Nice. Very interesting variations that I’ve not really tried.
Var = result seems clear while out variable switch seems easier to miss in reading, but that’s just my initial thought. Where would you find the out variable more clear than [type]$Var = do-stuff?
[…] 3 ways to store and display PowerShell Variable simultaneously […]
[…] 3 ways to store and display PowerShell Variable simultaneously […]
[…] 3 ways to store and display PowerShell Variable simultaneously […]
[…] have written a blog post to demonstrate a few more approaches to ‘store and display PowerShell Variable […]