Problem
You want to store the output of a pipeline or command for later use, or to work with it in more detail.
Solution
To store output for later use, store the output of the command in a variable. You can access this information later, or even pass it down the pipeline as though it was the output of the original command:
PS >$result = 2 + 2 PS >$result 4 PS >$processes = GetProcess PS >$processes.Count 85 PS >$processes | WhereObject { $_.ID eq 0 }
Handles
NPM(K)
PM(K)
WS(K) VM(M)
CPU(s)
Id ProcessName
0
0
0
16
0
0 Idle
Discussion
Variables in PowerShell (and all other scripting and programming languages) let you store the output of something so that you can use it later. Avariable name starts with a dollar sign ($) and can be followed by nearly any character. Asmall set of characters have special meaning to PowerShell, so PowerShell provides a way to make variable names that include even these.
You can store the result of any pipeline or command in a variable to use it later. If that command generates simple data (such as a number or string), then the variable contains simple data. If the command generates rich data (such as the objects that represent system processes from the GetProcess cmdlet), then the variable contains that list of rich data. If the command (such as a traditional executable) generates plain text (such as the output of traditional executable), then the variable contains plain text.
If you’ve stored a large amount of data into a variable, but no longer need that data, assign the value $null (or anything else) to that variable so that PowerShell can release the memory it was using to store
that data.
In addition to variables that you create, PowerShell automatically defines several variables that represent things such as the location of your profile file, the process ID of PowerShell, and more.