Save output from a PowerShell pipeline to a variable

Learn how to save output from the end of a PowerShell pipeline to a variable.

John Savill

July 21, 2015

1 Min Read
Save output from a PowerShell pipeline to a variable

For more technical explainers on PowerShell, read our updated 2021 report: PowerShell 101: A Technical Explainer for IT Pros.

Q. How can I save the output of a pipeline of PowerShell to a variable?

A. Normally to save the output of a PowerShell command to a variable you can use:

$variable = 

If however you have a sequence of PowerShell commands and you wish to save the final output to a variable it may not seem obvious how to save it, i.e.

 | 

The output object of command2 will be saved to $variable. You may also use -outvariable with command2 however make sure you don't add $ to the variable and the output is also still sent to screen, i.e.

 |  -outvariable variable

 

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like