3 ways to Store & Display results in/from a Powershell variable at the same time


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.

  1. Using -OutVariable parameter

    1

  2. Variable squeezing

    Its a shame many of us ( including myself 😉 ) were not aware of this for quite long.

    3

  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

    2.2

    Or, something like

    2

 

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

3

If you know more such techniques, let me know and I’ll include them in this post. Cheers!

signature 

Advertisements

2 thoughts on “3 ways to Store & Display results in/from a Powershell variable at the same time

  1. 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?

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s