1
00:00:03,140 --> 00:00:05,750
So with that, let's jump into PowerShell one more time,

2
00:00:05,750 --> 00:00:11,040
and let me show you some of these other variable options in action.

3
00:00:11,040 --> 00:00:14,140
Let's first look at Tee‑Object.

4
00:00:14,140 --> 00:00:15,970
So I'll run my Get‑Vegetable command and say,

5
00:00:15,970 --> 00:00:17,500
Get me all the root vegetables,

6
00:00:17,500 --> 00:00:21,110
and I'm going to pipe them to Tee‑Object and use the

7
00:00:21,110 --> 00:00:23,190
‑variable parameter and give it a name of root.

8
00:00:23,190 --> 00:00:26,740
You can give it whatever name that you want.

9
00:00:26,740 --> 00:00:31,320
What Tee‑Object is doing is sending the output to two places.

10
00:00:31,320 --> 00:00:34,580
It sends one output to the screen so I can see my results,

11
00:00:34,580 --> 00:00:38,330
and it also saved the output to a ‑variable root.

12
00:00:38,330 --> 00:00:40,910
I encourage you to look at the help for Tee‑Object or some

13
00:00:40,910 --> 00:00:42,770
other things that you can do with it.

14
00:00:42,770 --> 00:00:44,870
But variables are what we care about for right now.

15
00:00:44,870 --> 00:00:50,140
If I type $root, I get the results of the variable.

16
00:00:50,140 --> 00:00:52,980
Now, yes, I could have done $root = Get‑Vegetable,

17
00:00:52,980 --> 00:00:55,040
but then I'd have to take an extra step.

18
00:00:55,040 --> 00:00:58,380
This is just a way of using Tee‑Object, which has an alias of Tee,

19
00:00:58,380 --> 00:01:03,230
by the way, of making it easy to kind of get your cake and eat it too,

20
00:01:03,230 --> 00:01:06,910
get the results and save the results to a variable.

21
00:01:06,910 --> 00:01:09,190
So with the results, I can then say, Hey,

22
00:01:09,190 --> 00:01:12,850
I want to sort them on the count in descending order

23
00:01:12,850 --> 00:01:16,310
without having to rerun the original command.

24
00:01:16,310 --> 00:01:18,100
And, again, look at help for Tee‑Object,

25
00:01:18,100 --> 00:01:23,540
you can see there are some other ways that you might want to use it.

26
00:01:23,540 --> 00:01:28,640
Now I want to look at the common parameter, ‑OutVariable.

27
00:01:28,640 --> 00:01:30,150
So let's go back and look at Get‑Vegetable,

28
00:01:30,150 --> 00:01:34,750
and you should see this in pretty much any PowerShell command.

29
00:01:34,750 --> 00:01:38,900
Notice that CommonParameters section there in the help.

30
00:01:38,900 --> 00:01:44,670
When you see that, that means that the command will take ‑OutVariable,

31
00:01:44,670 --> 00:01:48,430
which has an alias of ‑ov.

32
00:01:48,430 --> 00:01:53,440
So now that I know that this command can do that, here's what we can do.

33
00:01:53,440 --> 00:01:57,240
So I can run Get‑Vegetable ‑OutVariable v.

34
00:01:57,240 --> 00:02:01,160
The nice thing about using ‑OutVariable is that you can save the output of

35
00:02:01,160 --> 00:02:05,540
individual segments of your pipeline expression to a variable.

36
00:02:05,540 --> 00:02:09,740
So the output of Get‑Vegetable will be saved to $v.

37
00:02:09,740 --> 00:02:12,250
I'm going to pipe this to where‑object where the count is

38
00:02:12,250 --> 00:02:17,640
greater than 10 and save that output to $w.

39
00:02:17,640 --> 00:02:19,450
So there are the full results.

40
00:02:19,450 --> 00:02:23,890
But I also now have variables $v and $w,

41
00:02:23,890 --> 00:02:27,840
so $v I can say, Hey, sort me on the name.

42
00:02:27,840 --> 00:02:30,340
So $v is all the vegetables.

43
00:02:30,340 --> 00:02:34,100
So there are the vegetables I got from the first part saved as $v.

44
00:02:34,100 --> 00:02:38,360
And $w is the where‑object part.

45
00:02:38,360 --> 00:02:42,890
Hey, I want to show the filtered objects,

46
00:02:42,890 --> 00:02:46,940
so that's filtered vegetables, and sort them on the count property.

47
00:02:46,940 --> 00:02:48,180
Very handy technique.

48
00:02:48,180 --> 00:02:49,330
I use this quite a bit.

49
00:02:49,330 --> 00:02:52,770
It's very useful if you're trying to troubleshoot or want to

50
00:02:52,770 --> 00:02:56,060
work with individual chunks of data separately.

51
00:02:56,060 --> 00:02:58,960
You can even append to that value.

52
00:02:58,960 --> 00:02:59,790
Let me show you how.

53
00:02:59,790 --> 00:03:01,900
So I'm going to do get‑service and find all the

54
00:03:01,900 --> 00:03:03,880
services that start with the letter b.

55
00:03:03,880 --> 00:03:06,310
And here I'm using the parameter alias ‑ov.

56
00:03:06,310 --> 00:03:09,240
We'll save that to b.

57
00:03:09,240 --> 00:03:10,940
So there are the results.

58
00:03:10,940 --> 00:03:17,340
And if I look at $b, I get the same results.

59
00:03:17,340 --> 00:03:18,780
I'm going to rerun the command.

60
00:03:18,780 --> 00:03:21,260
Thus, I'm getting all the c services,

61
00:03:21,260 --> 00:03:23,980
and I'm going to send them also to ‑OutVariable.

62
00:03:23,980 --> 00:03:27,580
But notice that plus sign in front of b.

63
00:03:27,580 --> 00:03:32,640
So this is going to append this output to $b.

64
00:03:32,640 --> 00:03:35,140
So here's the output from the command.

65
00:03:35,140 --> 00:03:37,260
Now if I look at $b,

66
00:03:37,260 --> 00:03:41,640
I can see there all my b services and then all of the c services.

67
00:03:41,640 --> 00:03:48,340
They've all been added together to that common variable $b that I created.

68
00:03:48,340 --> 00:03:50,320
There is a help topic that you can look at,

69
00:03:50,320 --> 00:03:54,760
help about_CommonParameters, give you a little more detail about ‑OutVariable,

70
00:03:54,760 --> 00:03:59,940
as well as some other parameters that you might want to take advantage of.

71
00:03:59,940 --> 00:04:02,220
The last variable option I want to show you is

72
00:04:02,220 --> 00:04:05,690
‑pipelinevariable. Now this is an advanced concept.

73
00:04:05,690 --> 00:04:08,160
This is not something that you will need to use all the time.

74
00:04:08,160 --> 00:04:11,120
It's kind of a special use case scenario.

75
00:04:11,120 --> 00:04:15,660
It's really designed when you have a long‑running PowerShell

76
00:04:15,660 --> 00:04:19,270
expression and you need to get information from one part of a

77
00:04:19,270 --> 00:04:22,420
pipeline into another part of the pipeline.

78
00:04:22,420 --> 00:04:24,510
This is what fixes that problem.

79
00:04:24,510 --> 00:04:28,550
‑OutVariable only writes the results and finishes that

80
00:04:28,550 --> 00:04:30,470
variable after that segment completes,

81
00:04:30,470 --> 00:04:34,320
and you cannot reference that variable inside the pipeline.

82
00:04:34,320 --> 00:04:39,050
You can only reference it after the whole expression has finished.

83
00:04:39,050 --> 00:04:41,140
So here's how this works.

84
00:04:41,140 --> 00:04:43,730
And it will really help if you can try to visualize,

85
00:04:43,730 --> 00:04:46,410
and I'll try to explain what Powershell doing here,

86
00:04:46,410 --> 00:04:49,540
if you can visualize what is going on.

87
00:04:49,540 --> 00:04:54,840
So we're going to start with the numbers 1 to 10 using the range operator.

88
00:04:54,840 --> 00:04:59,740
For each of those numbers, I'm just going to write it to the pipeline.

89
00:04:59,740 --> 00:05:02,340
That's what's in the curly braces.

90
00:05:02,340 --> 00:05:04,610
I'm also using the ‑pipelinevariable,

91
00:05:04,610 --> 00:05:12,140
so I'm going to save the output of this little segment to $a.

92
00:05:12,140 --> 00:05:16,770
The numbers are then going to be written onto the pipeline to the next step,

93
00:05:16,770 --> 00:05:20,910
which is another ForEach‑Object where I'm going to multiply

94
00:05:20,910 --> 00:05:23,360
the number that comes through the pipeline,

95
00:05:23,360 --> 00:05:27,720
the $_, by the value of $a.

96
00:05:27,720 --> 00:05:31,000
Now this is where the ‑pipelinevariable gets a little tricky.

97
00:05:31,000 --> 00:05:35,540
‑Pipelinevariable is basically going sequentially.

98
00:05:35,540 --> 00:05:40,440
So $a will have the numbers 1 through 10.

99
00:05:40,440 --> 00:05:42,570
So it's got 10 items.

100
00:05:42,570 --> 00:05:48,870
So when I go through the ForEachObject for the second time, $b is going

101
00:05:48,870 --> 00:05:52,830
to take the number and then get me the first number and then get the

102
00:05:52,830 --> 00:05:57,000
first element of $a, the second number and the second element of $a. So

103
00:05:57,000 --> 00:06:01,940
it lines everything up. In this case, the effect will be multiplying

104
00:06:01,940 --> 00:06:09,010
each number by itself. And then that result will be saved to

105
00:06:09,010 --> 00:06:13,440
Select‑Object where I'm going to create some custom properties, so the

106
00:06:13,440 --> 00:06:16,020
value will be $a.

107
00:06:16,020 --> 00:06:19,610
So I'm using the ‑pipelinevariable from the first part,

108
00:06:19,610 --> 00:06:22,930
which would be the numbers 1 through 10.

109
00:06:22,930 --> 00:06:26,840
I'll then create a second custom property called Square.

110
00:06:26,840 --> 00:06:31,040
And that's the value from $b.

111
00:06:31,040 --> 00:06:33,930
And then I'm going to create another property called Cubed,

112
00:06:33,930 --> 00:06:38,140
where I can take $b*$a.

113
00:06:38,140 --> 00:06:39,840
And there are the results.

114
00:06:39,840 --> 00:06:42,850
So if you can kind of visualize what PowerShell is

115
00:06:42,850 --> 00:06:45,740
doing, that sort of makes sense.

116
00:06:45,740 --> 00:06:48,620
I'll admit this is a bit of an artificial example.

117
00:06:48,620 --> 00:06:52,850
I tried finding some real‑world examples, and I really

118
00:06:52,850 --> 00:06:55,610
don't have many in my own library.

119
00:06:55,610 --> 00:06:57,600
I asked some other people and they'd say, Yeah,

120
00:06:57,600 --> 00:07:00,950
I really don't use it that much because there are other ways to

121
00:07:00,950 --> 00:07:04,140
use PowerShell that don't have to rely on the ‑pipelinevariable.

122
00:07:04,140 --> 00:07:07,920
Not everything in PowerShell has to be written as a one‑line

123
00:07:07,920 --> 00:07:09,740
expression as I've done here.

124
00:07:09,740 --> 00:07:13,510
Sometimes it is easier and makes more sense to break

125
00:07:13,510 --> 00:07:15,200
things up into multiple steps.

126
00:07:15,200 --> 00:07:19,170
So let's do this. I'm going to basically recreate the output but through

127
00:07:19,170 --> 00:07:22,340
multiple steps, and this might be easier to follow.

128
00:07:22,340 --> 00:07:28,610
So $a is 1 through 10. $b takes $a, and then for

129
00:07:28,610 --> 00:07:31,340
each one multiplies it by itself.

130
00:07:31,340 --> 00:07:34,000
So this is kind of behind the scenes what the ‑pipelinevariable is

131
00:07:34,000 --> 00:07:39,260
doing. I'm just doing it explicitly step by step in PowerShell. So

132
00:07:39,260 --> 00:07:41,730
now I'm going to use a scripting construct,

133
00:07:41,730 --> 00:07:45,750
although you can use it in the PowerShell pipeline, the for construct.

134
00:07:45,750 --> 00:07:53,780
So I'm going to go through and get the index of $b and select and create

135
00:07:53,780 --> 00:08:04,780
my custom objects here, and I'm going to create the Cubed value. And

136
00:08:04,780 --> 00:08:09,470
there we go. So I get the same results whether the second example with

137
00:08:09,470 --> 00:08:12,370
it broken out step by step makes more sense to you or the

138
00:08:12,370 --> 00:08:14,700
‑pipelinevariable makes more sense.

139
00:08:14,700 --> 00:08:17,920
That's fine. That's really kind of up to you. Again,

140
00:08:17,920 --> 00:08:20,080
this is not something you're going to use all the time,

141
00:08:20,080 --> 00:08:24,150
but I at least wanted to make you aware of it because you may see someone

142
00:08:24,150 --> 00:08:28,280
using ‑pipelinevariable, and I want you to understand what it is that they

143
00:08:28,280 --> 00:08:34,340
are doing. And that wraps up our exploration of variables in PowerShell.

144
00:08:34,340 --> 00:08:36,610
They're really not that difficult.

145
00:08:36,610 --> 00:08:39,870
$a equals foo, and then use $a.

146
00:08:39,870 --> 00:08:42,360
Save the results of a long‑running command to a variable.

147
00:08:42,360 --> 00:08:43,510
You're good to go.

148
00:08:43,510 --> 00:08:45,560
It's really not that complicated.

149
00:08:45,560 --> 00:08:49,440
I may have made it seem a little more complicated because I wanted to show

150
00:08:49,440 --> 00:08:54,270
you all of the commands and give you some insights into the nuances of using

151
00:08:54,270 --> 00:08:56,240
variables and the ways that it can trip you up,

152
00:08:56,240 --> 00:08:58,560
but also how it can make your life easier.

153
00:08:58,560 --> 00:09:00,080
So take a quick break,

154
00:09:00,080 --> 00:09:07,000
refill your beverage, and then come back, and let's learn more about the PowerShell language.

