1
00:00:01,840 --> 00:00:05,190
All of this will be much easier to understand when you see it in action,

2
00:00:05,190 --> 00:00:10,540
so let's head back to PowerShell and look at objects in the pipeline.

3
00:00:10,540 --> 00:00:12,540
Now, if you haven't already done so,

4
00:00:12,540 --> 00:00:14,900
if you want to follow along with these demonstrations,

5
00:00:14,900 --> 00:00:17,760
you'll need the PSTeachingTools module which I talked

6
00:00:17,760 --> 00:00:21,340
about and showed you in the last module,

7
00:00:21,340 --> 00:00:23,690
because I'm going to run Get‑Vegetable.

8
00:00:23,690 --> 00:00:26,940
We're going to demo all of these concepts with my generic

9
00:00:26,940 --> 00:00:30,700
command so that you can focus on the concepts and not get

10
00:00:30,700 --> 00:00:33,240
too caught up on the technology.

11
00:00:33,240 --> 00:00:36,340
So I run Get‑Vegetable and I get a result.

12
00:00:36,340 --> 00:00:38,840
Now, what type of object is that?

13
00:00:38,840 --> 00:00:43,940
Well, let's rerun Get‑Vegetable and pipe it to Get‑Member.

14
00:00:43,940 --> 00:00:50,440
So there you can see the TypeName is PSTeachingTools.PSVegetable object,

15
00:00:50,440 --> 00:00:54,560
and you can see the methods and the properties.

16
00:00:54,560 --> 00:00:54,670
Now,

17
00:00:54,670 --> 00:00:59,510
some of the methods that PowerShell will display like Equals and GetHashCode,

18
00:00:59,510 --> 00:01:02,740
PowerShell adds to all objects,

19
00:01:02,740 --> 00:01:06,640
and that the more you look at this you'll start recognizing those things.

20
00:01:06,640 --> 00:01:09,970
The methods that are unique to these object types,

21
00:01:09,970 --> 00:01:14,500
in this case my PSVegetable object, are things like Peel and Prepare,

22
00:01:14,500 --> 00:01:17,290
and then you can see the properties like Color,

23
00:01:17,290 --> 00:01:19,340
CookedState, Count.

24
00:01:19,340 --> 00:01:21,060
You can limit the results.

25
00:01:21,060 --> 00:01:22,990
This isn't too bad because it's a small object,

26
00:01:22,990 --> 00:01:26,940
but let's do Get‑Vegetable, pipe it again to Get‑Member,

27
00:01:26,940 --> 00:01:28,850
and this time I'm going to tell it, hey,

28
00:01:28,850 --> 00:01:31,640
only show me the properties.

29
00:01:31,640 --> 00:01:33,330
When you get to stuff like process objects,

30
00:01:33,330 --> 00:01:33,980
for example,

31
00:01:33,980 --> 00:01:38,170
you'll want to limit the results so you're not scrolling back and forth.

32
00:01:38,170 --> 00:01:44,240
So there we go, there are just the properties of my object, a little bit easier.

33
00:01:44,240 --> 00:01:50,690
So, let's run Get‑Vegetable, and let's just do corn.

34
00:01:50,690 --> 00:01:54,040
Now, look at that output.

35
00:01:54,040 --> 00:01:57,000
So the output, you see UPC, Count, Name,

36
00:01:57,000 --> 00:01:58,020
State, and Color,

37
00:01:58,020 --> 00:02:02,980
but you don't necessarily see all of those property names in the

38
00:02:02,980 --> 00:02:05,110
list there that you see with the properties,

39
00:02:05,110 --> 00:02:05,900
right?

40
00:02:05,900 --> 00:02:08,510
For example, there's no IsPeeled.

41
00:02:08,510 --> 00:02:13,840
That's part of the object, but it's not part of the default display.

42
00:02:13,840 --> 00:02:17,140
We're going to do more with properties in the next module.

43
00:02:17,140 --> 00:02:18,610
Let's look at methods now.

44
00:02:18,610 --> 00:02:21,040
So, again, we'll go back to Get‑Vegetable,

45
00:02:21,040 --> 00:02:25,500
pipe to Get‑Member, and this time we'll tell it only show me Methods,

46
00:02:25,500 --> 00:02:31,240
Peel, Prepare, those are the methods of this particular object.

47
00:02:31,240 --> 00:02:31,480
Now,

48
00:02:31,480 --> 00:02:34,960
we don't have to rely on these methods in order to

49
00:02:34,960 --> 00:02:39,170
manipulate the object, look for commands, and in

50
00:02:39,170 --> 00:02:41,610
PowerShell's case with my vegetable commands,

51
00:02:41,610 --> 00:02:44,580
if there's a Get, well, there is a corresponding Set.

52
00:02:44,580 --> 00:02:46,490
Not always, but often there is.

53
00:02:46,490 --> 00:02:49,450
So, I can try, hey, I could use get command and say,

54
00:02:49,450 --> 00:02:52,050
hey, is there a Set‑Vegetable, or just look for help,

55
00:02:52,050 --> 00:02:55,940
and sure enough there is a Set‑Vegetable cmdlet,

56
00:02:55,940 --> 00:03:00,140
and you can see the parameters there on how to use it.

57
00:03:00,140 --> 00:03:01,600
So let's go ahead and use it.

58
00:03:01,600 --> 00:03:04,440
Let's do Set‑Vegetable,

59
00:03:04,440 --> 00:03:09,410
and we'll use the name for corn, we'll set the cookingstate to grilled,

60
00:03:09,410 --> 00:03:14,160
and because this has ‑Whatif, and we talked about this a little bit last time,

61
00:03:14,160 --> 00:03:15,950
let's do ‑Whatif.

62
00:03:15,950 --> 00:03:20,150
So ‑Whatif tells PowerShell, I'm not going to make the change,

63
00:03:20,150 --> 00:03:23,540
but this is what I would have done if you let me do it, and it would have

64
00:03:23,540 --> 00:03:29,940
set the cookingstate to grilled on the vegetable corn.

65
00:03:29,940 --> 00:03:32,740
So I'm going to retype this command,

66
00:03:32,740 --> 00:03:37,340
and let's go ahead and actually do it without ‑Whatif.

67
00:03:37,340 --> 00:03:38,370
Did it change?

68
00:03:38,370 --> 00:03:39,640
Well, I don't know.

69
00:03:39,640 --> 00:03:40,650
I didn't get a result.

70
00:03:40,650 --> 00:03:42,940
Why did I not get a result?

71
00:03:42,940 --> 00:03:46,240
Well, if you look back up in the help, see that ‑Passthru?

72
00:03:46,240 --> 00:03:48,110
Yeah, if you see that,

73
00:03:48,110 --> 00:03:51,290
that's your clue this command is not going to write an

74
00:03:51,290 --> 00:03:54,140
object to the pipeline by default.

75
00:03:54,140 --> 00:03:58,720
I need to use ‑Passthru if I want to force it to show me

76
00:03:58,720 --> 00:04:01,940
the object that it is working with.

77
00:04:01,940 --> 00:04:04,110
And we can try this again here.

78
00:04:04,110 --> 00:04:05,740
Let's do Get‑Vegetable,

79
00:04:05,740 --> 00:04:08,650
and now you can see that it actually did change the state,

80
00:04:08,650 --> 00:04:11,740
because now it is showing up as Grilled.

81
00:04:11,740 --> 00:04:14,640
And if we look at help for Set‑Vegetable,

82
00:04:14,640 --> 00:04:17,090
and we can see there is a ‑Passthru parameter,

83
00:04:17,090 --> 00:04:21,410
and it tells you there in the help, Write object to the pipeline.

84
00:04:21,410 --> 00:04:22,360
Let's try this here.

85
00:04:22,360 --> 00:04:25,640
Let's do Get‑Vegetable, and let's just get the root vegetables.

86
00:04:25,640 --> 00:04:29,050
We're going to pipe all of those vegetables to Set‑Vegetable,

87
00:04:29,050 --> 00:04:33,890
and change the CookingState to Roasted, we're going to change the Count to 5,

88
00:04:33,890 --> 00:04:37,540
and now I'll use ‑Passthru.

89
00:04:37,540 --> 00:04:38,350
See the difference?

90
00:04:38,350 --> 00:04:43,100
So now ‑Passthru gives me the object that Set‑Vegetable worked on,

91
00:04:43,100 --> 00:04:45,770
and you can see that it changed the Count and it

92
00:04:45,770 --> 00:04:49,240
changed the CookingState of that object.

93
00:04:49,240 --> 00:04:52,960
There's no scripting there, I just told PowerShell get these things,

94
00:04:52,960 --> 00:04:56,040
do this with it, show me the results.

95
00:04:56,040 --> 00:04:58,810
No scripting, PowerShell did that all at once.

96
00:04:58,810 --> 00:05:02,940
I did it for, you know, four objects here.

97
00:05:02,940 --> 00:05:04,280
It could have been 4,000.

98
00:05:04,280 --> 00:05:05,530
It's the same command.

99
00:05:05,530 --> 00:05:07,420
This is what I'm talking about when I get excited

100
00:05:07,420 --> 00:05:10,850
about PowerShell managing at scale.

101
00:05:10,850 --> 00:05:15,840
Let's try one more example here with the PowerShell pipeline.

102
00:05:15,840 --> 00:05:21,240
I have a text file in my work folder called services.txt.

103
00:05:21,240 --> 00:05:23,710
It's just a list of services.

104
00:05:23,710 --> 00:05:26,990
Now I'm going to create an audit trail.

105
00:05:26,990 --> 00:05:33,630
I want to get the services, restart them, and save the results to a text file.

106
00:05:33,630 --> 00:05:37,920
In order to show you how this all works, let's break it down into simple steps.

107
00:05:37,920 --> 00:05:41,870
So Get‑Service has a Name parameter, which we have seen.

108
00:05:41,870 --> 00:05:47,140
And we can look at help here and see that it does accept pipeline input.

109
00:05:47,140 --> 00:05:47,530
Good.

110
00:05:47,530 --> 00:05:52,110
And I can test this very easily, just take a simple service like Lanmanserver,

111
00:05:52,110 --> 00:05:57,030
pipe to Get‑Service, and sure enough, it works.

112
00:05:57,030 --> 00:06:00,940
What I want you to do as I walk through this process is see if

113
00:06:00,940 --> 00:06:04,550
you can visualize what PowerShell is doing.

114
00:06:04,550 --> 00:06:06,450
Really important skill, I think,

115
00:06:06,450 --> 00:06:10,940
that will make PowerShell easier if you can do it.

116
00:06:10,940 --> 00:06:14,340
So we're going to do Get‑Content, the command I just ran.

117
00:06:14,340 --> 00:06:15,800
So I'm going to get the content.

118
00:06:15,800 --> 00:06:17,810
You already saw what it's going to do, right?

119
00:06:17,810 --> 00:06:21,110
It's going to give me a list of service names.

120
00:06:21,110 --> 00:06:24,690
I'm then going to pipe that, and when this happens,

121
00:06:24,690 --> 00:06:28,280
by the way, when you get that little double arrow there,

122
00:06:28,280 --> 00:06:30,580
that's PowerShell's way of telling you, hey,

123
00:06:30,580 --> 00:06:32,420
there must be more to the command,

124
00:06:32,420 --> 00:06:34,850
because I can't figure out what you want me to do.

125
00:06:34,850 --> 00:06:38,710
So if you're running a command and you aren't expecting to see this,

126
00:06:38,710 --> 00:06:40,260
you're thinking, hey, how come you're not running,

127
00:06:40,260 --> 00:06:45,530
you probably forgot closing curly brace or parenthesis or some piece of

128
00:06:45,530 --> 00:06:49,460
punctuation PowerShell doesn't understand what you are doing.

129
00:06:49,460 --> 00:06:50,140
In my case,

130
00:06:50,140 --> 00:06:55,010
I intentionally wanted to start each command on its own line to

131
00:06:55,010 --> 00:06:58,660
try to reinforce what the pipeline is doing.

132
00:06:58,660 --> 00:07:00,340
So we're going to get the content,

133
00:07:00,340 --> 00:07:02,550
we're then going to pipe it to another command,

134
00:07:02,550 --> 00:07:03,940
Get‑Service.

135
00:07:03,940 --> 00:07:06,240
We already know what that's going to do, right?

136
00:07:06,240 --> 00:07:12,140
It's going to take his output and pipe it to another command.

137
00:07:12,140 --> 00:07:16,840
We're going to pipe to Restart‑Service, and I'm using ‑Passthru.

138
00:07:16,840 --> 00:07:18,450
That's your clue, remember,

139
00:07:18,450 --> 00:07:21,560
that that command does not write objects to the pipeline.

140
00:07:21,560 --> 00:07:26,650
But I need objects to the pipeline, because if I don't use ‑Passthru,

141
00:07:26,650 --> 00:07:30,790
this next line where I'm going to create the file won't do anything.

142
00:07:30,790 --> 00:07:32,460
It'll create an empty file.

143
00:07:32,460 --> 00:07:38,510
Using ‑Passthru tells PowerShell, take the service objects that you restarted,

144
00:07:38,510 --> 00:07:41,130
send them onto the pipeline, and in this case,

145
00:07:41,130 --> 00:07:46,140
send them to Out‑File, which does its thing and creates the text file.

146
00:07:46,140 --> 00:07:51,580
PowerShell is doing its thing, I got no errors, so it must have worked.

147
00:07:51,580 --> 00:07:55,740
And if I look at the content of my restart.txt,

148
00:07:55,740 --> 00:08:00,290
there are the service objects that Restart‑Service wrote to the pipeline,

149
00:08:00,290 --> 00:08:03,240
which I captured with Out‑File.

150
00:08:03,240 --> 00:08:05,760
When you're learning and working with PowerShell

151
00:08:05,760 --> 00:08:09,590
and just kind of getting started, don't try to write,

152
00:08:09,590 --> 00:08:11,840
you know, this entire command that I just showed you.

153
00:08:11,840 --> 00:08:13,520
Don't try to write it all at once.

154
00:08:13,520 --> 00:08:16,460
That's what I was saying, you may think you know what you're doing,

155
00:08:16,460 --> 00:08:19,420
but a lot of beginners, there's a lot you're learning,

156
00:08:19,420 --> 00:08:21,890
and so if it doesn't work, you wont know,

157
00:08:21,890 --> 00:08:24,440
okay, which part of the command didn't work?

158
00:08:24,440 --> 00:08:26,140
Do the first part.

159
00:08:26,140 --> 00:08:27,190
Good, it works.

160
00:08:27,190 --> 00:08:28,540
Pipe onto the next part.

161
00:08:28,540 --> 00:08:29,510
Good, that worked.

162
00:08:29,510 --> 00:08:30,720
Then pipe onto the next part.

163
00:08:30,720 --> 00:08:32,640
Good, that works.

164
00:08:32,640 --> 00:08:37,780
As you slowly build experience, you'll be able to do more and more faster,

165
00:08:37,780 --> 00:08:40,410
but don't push yourself from the beginning.

166
00:08:40,410 --> 00:08:45,040
Try to visualize, what is PowerShell trying to do?

167
00:08:45,040 --> 00:08:48,070
Let me point out that PowerShell doesn't mean you have to write

168
00:08:48,070 --> 00:08:50,410
everything in a single pipeline expression,

169
00:08:50,410 --> 00:08:53,600
but taking advantage of the pipeline means that you can get a lot

170
00:08:53,600 --> 00:08:56,920
done with less work than the way we used to have to script with

171
00:08:56,920 --> 00:08:59,150
tools like VBScript or batch files.

172
00:08:59,150 --> 00:08:59,490
Of course,

173
00:08:59,490 --> 00:09:03,300
the best way that you can understand all of this is to try it yourself.

174
00:09:03,300 --> 00:09:05,540
If you make a mistake, pause,

175
00:09:05,540 --> 00:09:08,580
take a moment to read the error message, it most likely will tell you

176
00:09:08,580 --> 00:09:11,540
what to do to fix the problem, and then try again.

177
00:09:11,540 --> 00:09:15,050
I also encourage you to visualize or verbalize what

178
00:09:15,050 --> 00:09:17,430
you're trying to have PowerShell do.

179
00:09:17,430 --> 00:09:22,150
Often, when you say it out loud, it becomes easier to figure out what to do,

180
00:09:22,150 --> 00:09:23,950
and once you say it out loud,

181
00:09:23,950 --> 00:09:26,840
then it becomes easier to figure out what commands to use.

182
00:09:26,840 --> 00:09:29,320
And don't be afraid to reread help and examples.

183
00:09:29,320 --> 00:09:32,810
You need to get in the habit of reading and rereading help,

184
00:09:32,810 --> 00:09:35,540
even for commands that you think you understand.

185
00:09:35,540 --> 00:09:37,770
PowerShell is still under active development,

186
00:09:37,770 --> 00:09:40,840
and I really don't expect that to change any time soon.

187
00:09:40,840 --> 00:09:43,520
You're going to see new releases a few times a year,

188
00:09:43,520 --> 00:09:46,910
and the PowerShell team might introduce a new cmdlet or parameter,

189
00:09:46,910 --> 00:09:49,810
or they might update help with new examples.

190
00:09:49,810 --> 00:09:52,640
And don't forget the about help topic.

191
00:09:52,640 --> 00:09:55,720
Staying on top of the help will go a long way in helping you get the

192
00:09:55,720 --> 00:10:04,000
most out of working with objects in the pipeline. In the next module, we'll take this concept another step, so I'll see you there.

