So with that, let's jump into PowerShell one more time,
and let me show you some of these other variable options in action.
Let's first look at Tee‑Object.
So I'll run my Get‑Vegetable command and say,
Get me all the root vegetables,
and I'm going to pipe them to Tee‑Object and use the
‑variable parameter and give it a name of root.
You can give it whatever name that you want.
What Tee‑Object is doing is sending the output to two places.
It sends one output to the screen so I can see my results,
and it also saved the output to a ‑variable root.
I encourage you to look at the help for Tee‑Object or some
other things that you can do with it.
But variables are what we care about for right now.
If I type $root, I get the results of the variable.
Now, yes, I could have done $root = Get‑Vegetable,
but then I'd have to take an extra step.
This is just a way of using Tee‑Object, which has an alias of Tee,
by the way, of making it easy to kind of get your cake and eat it too,
get the results and save the results to a variable.
So with the results, I can then say, Hey,
I want to sort them on the count in descending order
without having to rerun the original command.
And, again, look at help for Tee‑Object,
you can see there are some other ways that you might want to use it.
Now I want to look at the common parameter, ‑OutVariable.
So let's go back and look at Get‑Vegetable,
and you should see this in pretty much any PowerShell command.
Notice that CommonParameters section there in the help.
When you see that, that means that the command will take ‑OutVariable,
which has an alias of ‑ov.
So now that I know that this command can do that, here's what we can do.
So I can run Get‑Vegetable ‑OutVariable v.
The nice thing about using ‑OutVariable is that you can save the output of
individual segments of your pipeline expression to a variable.
So the output of Get‑Vegetable will be saved to $v.
I'm going to pipe this to where‑object where the count is
greater than 10 and save that output to $w.
So there are the full results.
But I also now have variables $v and $w,
so $v I can say, Hey, sort me on the name.
So $v is all the vegetables.
So there are the vegetables I got from the first part saved as $v.
And $w is the where‑object part.
Hey, I want to show the filtered objects,
so that's filtered vegetables, and sort them on the count property.
Very handy technique.
I use this quite a bit.
It's very useful if you're trying to troubleshoot or want to
work with individual chunks of data separately.
You can even append to that value.
Let me show you how.
So I'm going to do get‑service and find all the
services that start with the letter b.
And here I'm using the parameter alias ‑ov.
We'll save that to b.
So there are the results.
And if I look at $b, I get the same results.
I'm going to rerun the command.
Thus, I'm getting all the c services,
and I'm going to send them also to ‑OutVariable.
But notice that plus sign in front of b.
So this is going to append this output to $b.
So here's the output from the command.
Now if I look at $b,
I can see there all my b services and then all of the c services.
They've all been added together to that common variable $b that I created.
There is a help topic that you can look at,
help about_CommonParameters, give you a little more detail about ‑OutVariable,
as well as some other parameters that you might want to take advantage of.
The last variable option I want to show you is
‑pipelinevariable. Now this is an advanced concept.
This is not something that you will need to use all the time.
It's kind of a special use case scenario.
It's really designed when you have a long‑running PowerShell
expression and you need to get information from one part of a
pipeline into another part of the pipeline.
This is what fixes that problem.
‑OutVariable only writes the results and finishes that
variable after that segment completes,
and you cannot reference that variable inside the pipeline.
You can only reference it after the whole expression has finished.
So here's how this works.
And it will really help if you can try to visualize,
and I'll try to explain what Powershell doing here,
if you can visualize what is going on.
So we're going to start with the numbers 1 to 10 using the range operator.
For each of those numbers, I'm just going to write it to the pipeline.
That's what's in the curly braces.
I'm also using the ‑pipelinevariable,
so I'm going to save the output of this little segment to $a.
The numbers are then going to be written onto the pipeline to the next step,
which is another ForEach‑Object where I'm going to multiply
the number that comes through the pipeline,
the $_, by the value of $a.
Now this is where the ‑pipelinevariable gets a little tricky.
‑Pipelinevariable is basically going sequentially.
So $a will have the numbers 1 through 10.
So it's got 10 items.
So when I go through the ForEachObject for the second time, $b is going
to take the number and then get me the first number and then get the
first element of $a, the second number and the second element of $a. So
it lines everything up. In this case, the effect will be multiplying
each number by itself. And then that result will be saved to
Select‑Object where I'm going to create some custom properties, so the
value will be $a.
So I'm using the ‑pipelinevariable from the first part,
which would be the numbers 1 through 10.
I'll then create a second custom property called Square.
And that's the value from $b.
And then I'm going to create another property called Cubed,
where I can take $b*$a.
And there are the results.
So if you can kind of visualize what PowerShell is
doing, that sort of makes sense.
I'll admit this is a bit of an artificial example.
I tried finding some real‑world examples, and I really
don't have many in my own library.
I asked some other people and they'd say, Yeah,
I really don't use it that much because there are other ways to
use PowerShell that don't have to rely on the ‑pipelinevariable.
Not everything in PowerShell has to be written as a one‑line
expression as I've done here.
Sometimes it is easier and makes more sense to break
things up into multiple steps.
So let's do this. I'm going to basically recreate the output but through
multiple steps, and this might be easier to follow.
So $a is 1 through 10. $b takes $a, and then for
each one multiplies it by itself.
So this is kind of behind the scenes what the ‑pipelinevariable is
doing. I'm just doing it explicitly step by step in PowerShell. So
now I'm going to use a scripting construct,
although you can use it in the PowerShell pipeline, the for construct.
So I'm going to go through and get the index of $b and select and create
my custom objects here, and I'm going to create the Cubed value. And
there we go. So I get the same results whether the second example with
it broken out step by step makes more sense to you or the
‑pipelinevariable makes more sense.
That's fine. That's really kind of up to you. Again,
this is not something you're going to use all the time,
but I at least wanted to make you aware of it because you may see someone
using ‑pipelinevariable, and I want you to understand what it is that they
are doing. And that wraps up our exploration of variables in PowerShell.
They're really not that difficult.
$a equals foo, and then use $a.
Save the results of a long‑running command to a variable.
You're good to go.
It's really not that complicated.
I may have made it seem a little more complicated because I wanted to show
you all of the commands and give you some insights into the nuances of using
variables and the ways that it can trip you up,
but also how it can make your life easier.
So take a quick break,
refill your beverage, and then come back, and let's learn more about the PowerShell language.