So with that, let's jump into PowerShell and let me
show you variables in action.
Okay, I have a Windows 10 desktop running the latest build of
PowerShell 7.0, and so let's go through and look at some ways of
working with variables, just kind of the basics here.
First off, the most basic, Get‑Variable. This is a command you can run to
see all of the variables that are defined in your current PowerShell
session. Variables can be referenced by their name,
that you see there with a dollar sign in front of it.
For example, I can say show me the variable, $PSVersionTable.
This is a handy variable, by the way, because it shows you what
version you're running, so I'm running PowerShell version 7.1.3, and
you can see I'm on a Windows desktop. And there are other variables
like $pwd, which is the current working directory, or $HOME, which is
your user's home directory.
You can use Get‑Variable to get the variable value, but don't do this. This is
going to fail, and that's because even though we are in the habit of referencing
variables with their dollar sign in front of it,
the dollar sign is not technically part of the name.
So normally what you would want to do if you're using Get‑Variable is just the
name without the dollar sign, and then you can see the value.
This is an object, so if I pipe this to say Select all,
I can see all of the properties of this variable object.
So the variable itself is an object, but really what we
care about and what you're going to see is using the stuff
that is inside the variable.
A $pid is another built‑in variable.
This is the process ID of your current PowerShell session.
And just to show you, if I pipe $pid to Get‑Member, the value, because
it's an integer, you can see that the type name that Get‑Member shows me
is a System.Int32. It's not a variable object,
it's whatever is in that variable.
That's what really matters.
So I can use that variable as a placeholder instead of me having trying to
figure out what's my process ID, you just use $pid to Get‑Process, and I
can get that to work with really no problem.
There is an about topic that you should take some time
to look at, help about_Variables.
I'm not going to go through that.
You can run that command on your own.
Now, let's quickly look at creating some variables.
It's as simple as typing $, whatever the variable name is, equals
whatever you want to assign it a value to. So I'm going to set $a =
2, and I can reference that just by typing $a. I can create a new
variable $n, say, = to foo.
And you can use variables to hold the output from commands.
So I'm going to run a command called Get‑Vegetable.
This is part of a module of mine called PS Teaching Tools
that you can download from the PowerShell Gallery, I've got
notes in the course downloads.
This is a tool that I wrote just to provide me a generic
command that writes objects to the pipeline without anything
special like file or processes.
And if I look at that, I can see, sure enough there are some vegetable objects.
Now, technically, you can also create variables with New‑Variable.
So I can say I want to create a variable called ver and give it a value of 1,
and then reference that by $ver, and you can see that it is 1.
There are some advanced things that you can do with this command if you
need it, but really more of a special case, but let me at least demonstrate
them so you have them in the back of your head.
You can use New‑Variable to create a variable and give it an option, say
ReadOnly, provides a little safety mechanism from someone changing the value
or you accidentally changing the value of a variable.
Or you can also create a variable that is considered a Constant.
So I'll create a variable pi and give it a value of
3.14, and that is now a Constant.
So there's $user and $pi.
So you can see that they work just the way that you would
expect them to like any other variable.
But, let's try to change variables now.
So I've got $n, up above you see it was foo, now I'm going to change it to bar.
And that works really no problem at all, because I haven't done anything
special. Raise my version level to 2, and now $ver is 2.
But, now let's look at those special variables I
created, the user and pi variables.
If I try to assign a new value to user, such as Gladys,
I get an error, and that says, hey,
I can't do this because you made that read‑only or a constant.
Now in this case, because it's read‑only, there is an option that you can
do using Set‑Variable, and use the ‑force parameter. And I'm using
‑PassThru so it writes the new result to the pipeline.
This allows me to modify the value of that ReadOnly variable, but I had to
take specific steps and really tell PowerShell, hey,
I know what I'm doing and I want this value to chain.
However, this won't work with things that are constants.
That doesn't work.
You'll have to delete the variable and recreate it if you want to reuse the
variable with the name of pi. And real quickly on the subject of variable
names, give your variables names that are meaningful. If you're just working
interactively in the console, you know, they can be short,
because you don't want to have to type a lot, although you do
have tab expansion, but you want to make sure that the
variable names are meaningful to you.
They should just be alphanumeric, don't include spaces,
there's no need for dashes or underscores, just very simple
names should be all that you need.
If you want to get rid of a variable,
you can use Clear‑Variable. So this is going to leave the
variable, but it will remove the value. So you can see the
variable is still there, but it is empty.
There is a cmdlet called Remove‑Variable that will completely delete
the variable from your PowerShell session, and now the veg variable is
gone and will no longer be available to me.
If I try to run Get‑Variable,
you can see I get an error, well, not really an error, it
says I can't find a variable with that name.
Now, one thing that's important here, that did not change the source here.
If I can still do Get‑Vegetable c*, which is what I used to create the
vegetable variable, those things are still there, so the variable is just
kind of a placeholder to hold the results. It doesn't change, there's nothing really linked up or anything like that.