1
00:00:02,540 --> 00:00:06,830
So of course the first question becomes, what is a variable in PowerShell?

2
00:00:06,830 --> 00:00:09,940
A variable is really nothing more than a container,

3
00:00:09,940 --> 00:00:12,820
and don't confuse containers with Docker containers.

4
00:00:12,820 --> 00:00:18,740
It's just a bucket or a place to hold something that we get in PowerShell.

5
00:00:18,740 --> 00:00:21,940
The variable itself is empty.

6
00:00:21,940 --> 00:00:22,540
It's nothing.

7
00:00:22,540 --> 00:00:23,410
It's meaningless.

8
00:00:23,410 --> 00:00:28,040
It doesn't have anything to do with anything until we put something in it.

9
00:00:28,040 --> 00:00:32,250
And variables are what make PowerShell reusable and easy to use,

10
00:00:32,250 --> 00:00:36,120
so that's why it's important to learn how variables work.

11
00:00:36,120 --> 00:00:38,090
When you start PowerShell,

12
00:00:38,090 --> 00:00:43,940
you actually get a lot of built‑in and automatic or predefined variables.

13
00:00:43,940 --> 00:00:46,730
Obviously, you can also define your own variables,

14
00:00:46,730 --> 00:00:51,140
and with variables more than likely you can change the values of them.

15
00:00:51,140 --> 00:00:54,780
And that speaks to the name of this thing: it's a variable,

16
00:00:54,780 --> 00:00:56,790
so the value is variable.

17
00:00:56,790 --> 00:00:57,650
It varies.

18
00:00:57,650 --> 00:01:00,750
It can be foo one minute and then bar the next.

19
00:01:00,750 --> 00:01:03,490
In your PowerShell session, variables are persistent,

20
00:01:03,490 --> 00:01:07,380
but they're not persistent across PowerShell sessions.

21
00:01:07,380 --> 00:01:12,990
To create a variable, really all you need to do is assign a value.

22
00:01:12,990 --> 00:01:16,930
And there I'm using the equals sign, so I'm going to create a variable,

23
00:01:16,930 --> 00:01:21,840
and the name of the variable is a, and I'm giving it a value of 1.

24
00:01:21,840 --> 00:01:25,070
The dollar sign is only used when we want to

25
00:01:25,070 --> 00:01:27,530
reference that variable in PowerShell.

26
00:01:27,530 --> 00:01:32,280
So if I type $a, I can see that the value is 1.

27
00:01:32,280 --> 00:01:37,440
PowerShell shows me the value of the variable.

28
00:01:37,440 --> 00:01:44,190
If I want to change the value of a, I can just assign a new value of 2 to $a,

29
00:01:44,190 --> 00:01:47,340
or the variable a.

30
00:01:47,340 --> 00:01:49,670
So assigning it, very simple.

31
00:01:49,670 --> 00:01:50,860
And again,

32
00:01:50,860 --> 00:01:53,920
this will last for the duration of my PowerShell session or

33
00:01:53,920 --> 00:01:59,060
until I change the value of $a again because variables really

34
00:01:59,060 --> 00:02:03,840
are nothing more than placeholders, so you can create a variable,

35
00:02:03,840 --> 00:02:07,240
put something in it, and then use that variable.

36
00:02:07,240 --> 00:02:07,820
For example,

37
00:02:07,820 --> 00:02:09,970
I can run Get‑Vegetable and pipe it to

38
00:02:09,970 --> 00:02:12,940
Select‑Object and use the ‑First parameter.

39
00:02:12,940 --> 00:02:15,200
The ‑First parameter wants an integer saying how

40
00:02:15,200 --> 00:02:18,140
many objects do you want to select?

41
00:02:18,140 --> 00:02:22,560
Instead of me having to hard type 2, I'll just use the variable $a,

42
00:02:22,560 --> 00:02:25,220
and so the number 2, which is the value of a,

43
00:02:25,220 --> 00:02:28,890
gets plugged in and I get 2 objects that come back.

44
00:02:28,890 --> 00:02:32,040
So the variable is the placeholder.

45
00:02:32,040 --> 00:02:35,670
If I change the value of $a and rerun the command,

46
00:02:35,670 --> 00:02:38,040
then I'll get something different.

47
00:02:38,040 --> 00:02:41,390
For example, let's set $a equal to 5.

48
00:02:41,390 --> 00:02:46,440
Same variable, all I've done is just change the value that is inside it.

49
00:02:46,440 --> 00:02:48,340
Now I can run a command like Get‑Process.

50
00:02:48,340 --> 00:02:52,540
Same idea as Select‑Object ‑First $a.

51
00:02:52,540 --> 00:02:55,540
Whatever $a is, that's what I'll get.

52
00:02:55,540 --> 00:02:59,030
Now in this case I'm also going to do something else with the variable.

53
00:02:59,030 --> 00:03:03,250
I'm going to create a variable $b that will hold the contents of

54
00:03:03,250 --> 00:03:06,340
that Get‑Process Select‑Object expression.

55
00:03:06,340 --> 00:03:11,650
So I can use $b as a placeholder for that expression that created it.

56
00:03:11,650 --> 00:03:15,800
So instead of having to run Get‑Process, I can just reference $b.

57
00:03:15,800 --> 00:03:19,950
And then I can select the name properties of those objects.

58
00:03:19,950 --> 00:03:24,040
Now remember, $b only has the first five processes.

59
00:03:24,040 --> 00:03:27,440
The variable placeholder makes this really simple.

60
00:03:27,440 --> 00:03:28,010
In this case,

61
00:03:28,010 --> 00:03:32,560
you can use the placeholder $b for anything that you would want to do with it.

62
00:03:32,560 --> 00:03:36,020
So instead of having to run potentially a long‑running command,

63
00:03:36,020 --> 00:03:37,150
Get‑Process really isn't,

64
00:03:37,150 --> 00:03:41,440
but say I had some command that was going to run and take a long time to run,

65
00:03:41,440 --> 00:03:44,170
and I want to rework with that data, I don't want to rerun the

66
00:03:44,170 --> 00:03:47,080
command, save the results to a variable like $b,

67
00:03:47,080 --> 00:03:50,330
and now instead of selecting the name,

68
00:03:50,330 --> 00:03:55,390
all I say is give me the objects sorted by the working set property in

69
00:03:55,390 --> 00:03:58,140
descending order and then select some properties.

70
00:03:58,140 --> 00:04:01,040
So using $b makes that so much easier.

71
00:04:01,040 --> 00:04:05,530
I don't have to rerun Get‑Process or whatever the long‑running command is; I

72
00:04:05,530 --> 00:04:10,240
can just use the variable that is holding the results.

73
00:04:10,240 --> 00:04:13,550
As I said, this becomes very handy if you're running commands,

74
00:04:13,550 --> 00:04:15,120
say, like Get‑ADUser,

75
00:04:15,120 --> 00:04:20,090
where you may be returning 5,000 objects and that takes a long time to run,

76
00:04:20,090 --> 00:04:23,000
or Get‑EventLog or GetWinEvent, I should say,

77
00:04:23,000 --> 00:04:26,410
in PowerShell 7, that takes a long time to run.

78
00:04:26,410 --> 00:04:31,040
Saving to a variable makes your life a whole lot easier.

79
00:04:31,040 --> 00:04:34,980
Now there are also cmdlets that we can use to work with variables.

80
00:04:34,980 --> 00:04:38,720
Variable is the noun, so there's ways that we can get variables,

81
00:04:38,720 --> 00:04:42,960
set variables, create new ones, or remove them.

82
00:04:42,960 --> 00:04:46,250
Most of the time we're just going to be assigning

83
00:04:46,250 --> 00:04:47,810
values and changing the values.

84
00:04:47,810 --> 00:04:50,760
It's really that simple.

85
00:04:50,760 --> 00:04:55,360
Variables, when you create them, are generally independent.

86
00:04:55,360 --> 00:04:56,920
What I mean is,

87
00:04:56,920 --> 00:05:01,290
you can remove the variable without affecting the original source.

88
00:05:01,290 --> 00:05:05,900
And also, you can remove the original source without affecting the variable,

89
00:05:05,900 --> 00:05:09,340
depending on what commands that you are working with.

90
00:05:09,340 --> 00:05:14,350
Don't assume that the variable will update if the source updates.

91
00:05:14,350 --> 00:05:16,420
Sometimes it does; sometimes it doesn't.

92
00:05:16,420 --> 00:05:24,000
But you really need to test everything if you're using variables in this placeholder concept that I'm talking about.

