Save output from a PowerShell pipeline to a variable

Save output from a PowerShell pipeline to a variable

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

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish