1
00:00:02,140 --> 00:00:05,310
So with that, let's jump into PowerShell and let me

2
00:00:05,310 --> 00:00:09,140
show you variables in action.

3
00:00:09,140 --> 00:00:12,870
Okay, I have a Windows 10 desktop running the latest build of

4
00:00:12,870 --> 00:00:17,590
PowerShell 7.0, and so let's go through and look at some ways of

5
00:00:17,590 --> 00:00:20,340
working with variables, just kind of the basics here.

6
00:00:20,340 --> 00:00:24,200
First off, the most basic, Get‑Variable. This is a command you can run to

7
00:00:24,200 --> 00:00:28,930
see all of the variables that are defined in your current PowerShell

8
00:00:28,930 --> 00:00:33,580
session. Variables can be referenced by their name,

9
00:00:33,580 --> 00:00:35,990
that you see there with a dollar sign in front of it.

10
00:00:35,990 --> 00:00:41,040
For example, I can say show me the variable, $PSVersionTable.

11
00:00:41,040 --> 00:00:44,080
This is a handy variable, by the way, because it shows you what

12
00:00:44,080 --> 00:00:48,450
version you're running, so I'm running PowerShell version 7.1.3, and

13
00:00:48,450 --> 00:00:51,950
you can see I'm on a Windows desktop. And there are other variables

14
00:00:51,950 --> 00:00:57,320
like $pwd, which is the current working directory, or $HOME, which is

15
00:00:57,320 --> 00:01:02,840
your user's home directory.

16
00:01:02,840 --> 00:01:08,730
You can use Get‑Variable to get the variable value, but don't do this. This is

17
00:01:08,730 --> 00:01:13,100
going to fail, and that's because even though we are in the habit of referencing

18
00:01:13,100 --> 00:01:16,390
variables with their dollar sign in front of it,

19
00:01:16,390 --> 00:01:20,040
the dollar sign is not technically part of the name.

20
00:01:20,040 --> 00:01:23,250
So normally what you would want to do if you're using Get‑Variable is just the

21
00:01:23,250 --> 00:01:29,640
name without the dollar sign, and then you can see the value.

22
00:01:29,640 --> 00:01:35,340
This is an object, so if I pipe this to say Select all,

23
00:01:35,340 --> 00:01:39,380
I can see all of the properties of this variable object.

24
00:01:39,380 --> 00:01:42,350
So the variable itself is an object, but really what we

25
00:01:42,350 --> 00:01:45,800
care about and what you're going to see is using the stuff

26
00:01:45,800 --> 00:01:49,440
that is inside the variable.

27
00:01:49,440 --> 00:01:51,520
A $pid is another built‑in variable.

28
00:01:51,520 --> 00:01:58,140
This is the process ID of your current PowerShell session.

29
00:01:58,140 --> 00:02:03,820
And just to show you, if I pipe $pid to Get‑Member, the value, because

30
00:02:03,820 --> 00:02:07,320
it's an integer, you can see that the type name that Get‑Member shows me

31
00:02:07,320 --> 00:02:11,590
is a System.Int32. It's not a variable object,

32
00:02:11,590 --> 00:02:13,830
it's whatever is in that variable.

33
00:02:13,830 --> 00:02:16,240
That's what really matters.

34
00:02:16,240 --> 00:02:19,950
So I can use that variable as a placeholder instead of me having trying to

35
00:02:19,950 --> 00:02:23,940
figure out what's my process ID, you just use $pid to Get‑Process, and I

36
00:02:23,940 --> 00:02:27,700
can get that to work with really no problem.

37
00:02:27,700 --> 00:02:29,980
There is an about topic that you should take some time

38
00:02:29,980 --> 00:02:32,840
to look at, help about_Variables.

39
00:02:32,840 --> 00:02:34,230
I'm not going to go through that.

40
00:02:34,230 --> 00:02:37,100
You can run that command on your own.

41
00:02:37,100 --> 00:02:40,540
Now, let's quickly look at creating some variables.

42
00:02:40,540 --> 00:02:45,380
It's as simple as typing $, whatever the variable name is, equals

43
00:02:45,380 --> 00:02:49,940
whatever you want to assign it a value to. So I'm going to set $a =

44
00:02:49,940 --> 00:02:56,650
2, and I can reference that just by typing $a. I can create a new

45
00:02:56,650 --> 00:02:59,940
variable $n, say, = to foo.

46
00:02:59,940 --> 00:03:02,800
And you can use variables to hold the output from commands.

47
00:03:02,800 --> 00:03:05,760
So I'm going to run a command called Get‑Vegetable.

48
00:03:05,760 --> 00:03:09,890
This is part of a module of mine called PS Teaching Tools

49
00:03:09,890 --> 00:03:11,680
that you can download from the PowerShell Gallery, I've got

50
00:03:11,680 --> 00:03:13,530
notes in the course downloads.

51
00:03:13,530 --> 00:03:17,170
This is a tool that I wrote just to provide me a generic

52
00:03:17,170 --> 00:03:19,990
command that writes objects to the pipeline without anything

53
00:03:19,990 --> 00:03:22,640
special like file or processes.

54
00:03:22,640 --> 00:03:27,050
And if I look at that, I can see, sure enough there are some vegetable objects.

55
00:03:27,050 --> 00:03:33,140
Now, technically, you can also create variables with New‑Variable.

56
00:03:33,140 --> 00:03:37,720
So I can say I want to create a variable called ver and give it a value of 1,

57
00:03:37,720 --> 00:03:42,140
and then reference that by $ver, and you can see that it is 1.

58
00:03:42,140 --> 00:03:44,830
There are some advanced things that you can do with this command if you

59
00:03:44,830 --> 00:03:48,620
need it, but really more of a special case, but let me at least demonstrate

60
00:03:48,620 --> 00:03:51,840
them so you have them in the back of your head.

61
00:03:51,840 --> 00:03:56,210
You can use New‑Variable to create a variable and give it an option, say

62
00:03:56,210 --> 00:04:01,070
ReadOnly, provides a little safety mechanism from someone changing the value

63
00:04:01,070 --> 00:04:06,040
or you accidentally changing the value of a variable.

64
00:04:06,040 --> 00:04:10,080
Or you can also create a variable that is considered a Constant.

65
00:04:10,080 --> 00:04:12,630
So I'll create a variable pi and give it a value of

66
00:04:12,630 --> 00:04:17,540
3.14, and that is now a Constant.

67
00:04:17,540 --> 00:04:20,850
So there's $user and $pi.

68
00:04:20,850 --> 00:04:23,390
So you can see that they work just the way that you would

69
00:04:23,390 --> 00:04:26,440
expect them to like any other variable.

70
00:04:26,440 --> 00:04:29,020
But, let's try to change variables now.

71
00:04:29,020 --> 00:04:34,420
So I've got $n, up above you see it was foo, now I'm going to change it to bar.

72
00:04:34,420 --> 00:04:38,600
And that works really no problem at all, because I haven't done anything

73
00:04:38,600 --> 00:04:44,340
special. Raise my version level to 2, and now $ver is 2.

74
00:04:44,340 --> 00:04:47,600
But, now let's look at those special variables I

75
00:04:47,600 --> 00:04:51,840
created, the user and pi variables.

76
00:04:51,840 --> 00:04:56,080
If I try to assign a new value to user, such as Gladys,

77
00:04:56,080 --> 00:04:58,630
I get an error, and that says, hey,

78
00:04:58,630 --> 00:05:02,840
I can't do this because you made that read‑only or a constant.

79
00:05:02,840 --> 00:05:07,100
Now in this case, because it's read‑only, there is an option that you can

80
00:05:07,100 --> 00:05:13,500
do using Set‑Variable, and use the ‑force parameter. And I'm using

81
00:05:13,500 --> 00:05:17,270
‑PassThru so it writes the new result to the pipeline.

82
00:05:17,270 --> 00:05:22,740
This allows me to modify the value of that ReadOnly variable, but I had to

83
00:05:22,740 --> 00:05:25,110
take specific steps and really tell PowerShell, hey,

84
00:05:25,110 --> 00:05:29,340
I know what I'm doing and I want this value to chain.

85
00:05:29,340 --> 00:05:33,030
However, this won't work with things that are constants.

86
00:05:33,030 --> 00:05:34,100
That doesn't work.

87
00:05:34,100 --> 00:05:38,200
You'll have to delete the variable and recreate it if you want to reuse the

88
00:05:38,200 --> 00:05:42,180
variable with the name of pi. And real quickly on the subject of variable

89
00:05:42,180 --> 00:05:45,690
names, give your variables names that are meaningful. If you're just working

90
00:05:45,690 --> 00:05:48,500
interactively in the console, you know, they can be short,

91
00:05:48,500 --> 00:05:50,180
because you don't want to have to type a lot, although you do

92
00:05:50,180 --> 00:05:53,310
have tab expansion, but you want to make sure that the

93
00:05:53,310 --> 00:05:55,640
variable names are meaningful to you.

94
00:05:55,640 --> 00:05:59,080
They should just be alphanumeric, don't include spaces,

95
00:05:59,080 --> 00:06:02,750
there's no need for dashes or underscores, just very simple

96
00:06:02,750 --> 00:06:05,640
names should be all that you need.

97
00:06:05,640 --> 00:06:07,350
If you want to get rid of a variable,

98
00:06:07,350 --> 00:06:10,810
you can use Clear‑Variable. So this is going to leave the

99
00:06:10,810 --> 00:06:16,070
variable, but it will remove the value. So you can see the

100
00:06:16,070 --> 00:06:19,140
variable is still there, but it is empty.

101
00:06:19,140 --> 00:06:23,060
There is a cmdlet called Remove‑Variable that will completely delete

102
00:06:23,060 --> 00:06:29,380
the variable from your PowerShell session, and now the veg variable is

103
00:06:29,380 --> 00:06:32,560
gone and will no longer be available to me.

104
00:06:32,560 --> 00:06:37,040
If I try to run Get‑Variable,

105
00:06:37,040 --> 00:06:39,100
you can see I get an error, well, not really an error, it

106
00:06:39,100 --> 00:06:42,140
says I can't find a variable with that name.

107
00:06:42,140 --> 00:06:46,370
Now, one thing that's important here, that did not change the source here.

108
00:06:46,370 --> 00:06:51,920
If I can still do Get‑Vegetable c*, which is what I used to create the

109
00:06:51,920 --> 00:06:56,380
vegetable variable, those things are still there, so the variable is just

110
00:06:56,380 --> 00:07:05,000
kind of a placeholder to hold the results. It doesn't change, there's nothing really linked up or anything like that.

