Automatically assign command output to a variable in Powershell
Not exactly. However,
$Variable = (Some-Function)
will save the output of Some-Function
in the variable $Variable
, and allow you to then manipulate the data or pass it to other functions later on. To accomplish the equivalent of
Some-Function | Tee-Object -Variable $Variable
which would take the output of Some-Function
, save it in $variable
, and pass it through the pipe to the next command, you could use
$variable = (Some-Function)
$variable
(or $variable | Next-Command
), and accomplish what you appear to want.
Stadem
I'm a self-taught computer fan who started programming in VBA by playing around with Excel's macro recorder and drooling over every macro-enabled worksheet I could find. Working my way back to 'real' programming, I've helped build some data manipulation programs for my company in VB.NET. I've been exposed to C and C++, and am on the fence about moving in the C# direction. Interests (in order): Excel/VBA VB.NET C#
Updated on June 05, 2022Comments
-
Stadem almost 2 years
Is there a hack to encapsulate all commands in a Powershell session so that all output is tee'd to a temporary variable?
My problem arises when I enter a command
Some-Function
I'm fully aware of commands like
Tee-Object
and-OutVariable
that allow me to pipe a function's output to a variable as well as to the console. I could accomplish my goal with the following:Some-Function | Tee-Object -Variable PSMyCustomTempVariable
However, I often don't know or anticipate whether I need the variable until after I've run already run
Some-Function
. In this case, it'd be great to have a variable standing by that contains the output from the last function. This is especially helpful for functions that take a while to run like recursive file searches.I've looked at about_Logging as well as Start-Transcript, but these seem to be concerned with recording text to a file; I need the objects returned by the function.
So, is there any way to modify my session so that any Powershell command
Some-Function
basically turns intoSome-Function | Tee-Object -Variable PSMyCustomTempVariable
? -
Stadem about 6 yearsThanks Jeff; this is what I usually end up doing though. I'm looking for something that will assign the variable for me. Maybe a PSReadlineKeyHandler option, I'll check that out.
-
Jeff Zeitlin about 6 yearsI'm not sure what you mean by 'assign the variable for me'. What, specifically, is inadequate or unacceptable about this, which is the defined and standard way of doing what you want in PowerShell?
-
Stadem about 6 yearsI'm looking for a hack, not a standard method. The standard method requires a lot of extra typing for a variable I may not need. I'll try to modify the question accordingly.
-
Jeff Zeitlin about 6 yearsI don't see "a lot" of extra typing, but I also find that
($variable = (some-command)) | Next-Command
works as well. Point-blank, you are not going to get a 'tee' for this. PowerShell doesn't support it, unless you are going to write your own cmdlet from scratch. -
Stadem about 6 yearsOK fair enough :)