Now in addition to everything I've shown you about variables,
you know,
you've seen how easy it is to create them and use them as
placeholders and do variable expansion, there are some advanced things,
at least advanced from you as a beginner in PowerShell that
you might want to work with variables.
And these are things such as Tee‑Object,
which is a cmdlet,
which I'll show that allows you to basically pipe an
expression to the pipeline and also to a variable.
We're going to look in a moment here about OutVariable and then a more
complex and advanced topic that I debated putting in,
but I just want to show it to you because you may see this in examples,
and I want you to at least have a rough understanding of how this works,
and that is the PipelineVariable.
So Tee‑Object, which has an alias of Tee, is pretty simple to use.
You run any command you want on the left side of the pipe character, so I'm
going to do Get‑Process ls*, and pipe that to Tee‑Object,
and I'm going to assign the variable p to hold the results.
So what's going to happen is if I run this in a PowerShell session,
I will get the expression result, the process result will show on the screen,
and I'll also have it saved to a variable,
$p.
So what will happen is I can run Get‑Process,
see the results, and save the results to a variable.
It looks like this.
I get my processes that start with Ls,
and the results have been saved to a variable,
$p.
So I can take $p, it's the placeholder that holds the results, and say,
hey,
I want to measure say the working set and get the sum of
those ls processes. Pretty simple.
So using Tee‑Object is a nice way to get the results and also
save them to a variable all in one command.
Now kind of related to Tee‑Object is OutVariable.
OutVariable is a common parameter,
which means all PowerShell commands and advanced
functions will include ‑outvariable.
In this situation,
what's happening is I'm using $p that I had from my Tee‑Object example,
and then I'm going to pipe this again to measure‑object.
But this time, I'm going to save the results also to a variable, m.
So the only thing that goes to $m will be the results
of that measure‑object command.
This is a nice way of saving output from an individual pipeline segment.
It comes in very handy when you're troubleshooting or
debugging why a command does not work.
So I can use $m and say show me the sum property of that value.
Now I could've also used Tee‑Object, but I wanted to demonstrate OutVariable.
This is a parameter I use often in my PowerShell work and
something I think you'll use as well.
And there you can see, I have the value of the sum property of the variable $m,
which is the measurement object.
I hope you can kind of follow all of that.
The last item related to variables is another common
parameter called the PipelineVariable.
Now this is admittedly an advanced concept,
so don't feel that this makes your head hurt that you're
doing something wrong or you're not up to it.
I rarely use PipelineVariable.
This allows you to save pipeline segment output across the pipeline and
then reuse it in another part of the pipeline expression.
Sounds confusing.
It is confusing.
Don't worry too much about it,
but at least I want you to see what is happening here.
What we're getting is a temporary, in essence,
in‑memory variable,
so I only kind of walk through what's happening here on the screen.
So I'm getting the numbers 1 to 5.
So that's the range operator, the dot dot.
For each of those numbers, it's going to foreach‑object.
And foreach‑object I'm just displaying the value 12345.
That's in the parentheses, I'm sorry, that's in the curly braces and the {$_}.
However,
I'm also saving the output of foreach‑object as an in‑memory variable to $a,
‑pipelinevariable a.
So now I'm piping the numbers 1 through 5.
I then pipe those results to another foreach‑object and
save that variable to pipeline b.
And this pipeline expression is taking the number 1
through 5 and then multiplying it by 2.
And then at the very end,
I'm piping those results to another foreach‑object where
I'm just displaying { "$a * 2 = $b"}.
Now, in this case, I'm using that variable expansion to create a string.
I admit this is an artificial example,
but hopefully you can kind of see what is going on here.
Let's look at the results.
So $a is 12345, and that's as it comes through the pipeline,
$b is then the results of $a, or basically,
the first number times 2.
It gets a little complicated.
Again, this is for special use case scenarios.
It's an advanced topic you may not use all the time.
There are often other ways to get the job done
without resorting to PipelineVariable, but if you need to use it, it is there.