So of course the first question becomes, what is a variable in PowerShell?
A variable is really nothing more than a container,
and don't confuse containers with Docker containers.
It's just a bucket or a place to hold something that we get in PowerShell.
The variable itself is empty.
It's nothing.
It's meaningless.
It doesn't have anything to do with anything until we put something in it.
And variables are what make PowerShell reusable and easy to use,
so that's why it's important to learn how variables work.
When you start PowerShell,
you actually get a lot of built‑in and automatic or predefined variables.
Obviously, you can also define your own variables,
and with variables more than likely you can change the values of them.
And that speaks to the name of this thing: it's a variable,
so the value is variable.
It varies.
It can be foo one minute and then bar the next.
In your PowerShell session, variables are persistent,
but they're not persistent across PowerShell sessions.
To create a variable, really all you need to do is assign a value.
And there I'm using the equals sign, so I'm going to create a variable,
and the name of the variable is a, and I'm giving it a value of 1.
The dollar sign is only used when we want to
reference that variable in PowerShell.
So if I type $a, I can see that the value is 1.
PowerShell shows me the value of the variable.
If I want to change the value of a, I can just assign a new value of 2 to $a,
or the variable a.
So assigning it, very simple.
And again,
this will last for the duration of my PowerShell session or
until I change the value of $a again because variables really
are nothing more than placeholders, so you can create a variable,
put something in it, and then use that variable.
For example,
I can run Get‑Vegetable and pipe it to
Select‑Object and use the ‑First parameter.
The ‑First parameter wants an integer saying how
many objects do you want to select?
Instead of me having to hard type 2, I'll just use the variable $a,
and so the number 2, which is the value of a,
gets plugged in and I get 2 objects that come back.
So the variable is the placeholder.
If I change the value of $a and rerun the command,
then I'll get something different.
For example, let's set $a equal to 5.
Same variable, all I've done is just change the value that is inside it.
Now I can run a command like Get‑Process.
Same idea as Select‑Object ‑First $a.
Whatever $a is, that's what I'll get.
Now in this case I'm also going to do something else with the variable.
I'm going to create a variable $b that will hold the contents of
that Get‑Process Select‑Object expression.
So I can use $b as a placeholder for that expression that created it.
So instead of having to run Get‑Process, I can just reference $b.
And then I can select the name properties of those objects.
Now remember, $b only has the first five processes.
The variable placeholder makes this really simple.
In this case,
you can use the placeholder $b for anything that you would want to do with it.
So instead of having to run potentially a long‑running command,
Get‑Process really isn't,
but say I had some command that was going to run and take a long time to run,
and I want to rework with that data, I don't want to rerun the
command, save the results to a variable like $b,
and now instead of selecting the name,
all I say is give me the objects sorted by the working set property in
descending order and then select some properties.
So using $b makes that so much easier.
I don't have to rerun Get‑Process or whatever the long‑running command is; I
can just use the variable that is holding the results.
As I said, this becomes very handy if you're running commands,
say, like Get‑ADUser,
where you may be returning 5,000 objects and that takes a long time to run,
or Get‑EventLog or GetWinEvent, I should say,
in PowerShell 7, that takes a long time to run.
Saving to a variable makes your life a whole lot easier.
Now there are also cmdlets that we can use to work with variables.
Variable is the noun, so there's ways that we can get variables,
set variables, create new ones, or remove them.
Most of the time we're just going to be assigning
values and changing the values.
It's really that simple.
Variables, when you create them, are generally independent.
What I mean is,
you can remove the variable without affecting the original source.
And also, you can remove the original source without affecting the variable,
depending on what commands that you are working with.
Don't assume that the variable will update if the source updates.
Sometimes it does; sometimes it doesn't.
But you really need to test everything if you're using variables in this placeholder concept that I'm talking about.