July 24th, 2018

PowerShell PowerTip: What is the point of Out-Variable?

Kory Thacher
Premier Field Engineer

A lot of times people see others using the common parameter -OutVariable instead of the best practice $var = <value>. This leads to a lot of folks wondering why OutVariable  exists.

The real use for OutVariable is to save your data off, while still letting it get sent along the output stream. What this means is that you could use it somewhere inside of a pipe to save some data for later re-use, but continue doing other work with that data.


get-process | where {$_.handles -lt 100} -OutVariable LowHandles | foreach {"$($_.name) is using $($_.handles) handles"}
$LowHandles.count


fontdrvhost is using 44 handles
fontdrvhost is using 44 handles
Idle is using 0 handles
LsaIso is using 38 handles
Memory Compression is using 0 handles
Registry is using 0 handles
Secure System is using 0 handles
SgrmBroker is using 56 handles
smss is using 52 handles
svchost is using 84 handles
svchost is using 98 handles
vmnetdhcp is using 78 handles

12

Notice that even though the data continued being pipped along, I could also reference it later when I asked for the count.

Hope that helps, tune in more often to get short and sweet PowerTips!

Author

Kory Thacher
Premier Field Engineer

I've been a PFE since 2012, working with various technologies. I live in the modern applications domain, doing work in UWP apps, .NET, Unity, DevOps, and of course PowerShell. I've been teaching PowerShell related workshops very frequently for years, and I really enjoy getting the opportunity to explain a topic or learn something new from my colleagues. I enjoy scripting because of how fast and interactive it can be, and I love getting interesting problems to work on with customers. ...

More about author

0 comments

Discussion are closed.