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 = <command>
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.
<command1> | <command2>
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.
<command1> | <command2> -outvariable variable