In order to write less code or to write better one-liners in Powershell, I often find one of these techniques useful, and let me know if you feel the same.

Using -OutVariable parameter Variable squeezing Its a shame many of us ( including myself 😉 ) were not aware of this for quite long. 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 below Or, something like

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 know more such techniques, let me know and I’ll include them in this post. Cheers!

Follow @singhprateik