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.

  1. Using -OutVariable parameter

    PowerShell Variable assignment and display

  2. 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.

    PowerShell Variable squeezing

  3. 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

    PowerShell Variable using Tee-Object cmdlet

    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 like this article read similar articles under ‘N – Ways to’ category

Please do follow me on twitter and thanks for reading. Cheers! 😉


Subscribe to our mailing list

* indicates required