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



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"}

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


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!


Comments are closed. Login to edit/delete your existing comments