1
00:00:01,040 --> 00:00:03,490
Now in addition to everything I've shown you about variables,

2
00:00:03,490 --> 00:00:03,670
you know,

3
00:00:03,670 --> 00:00:07,120
you've seen how easy it is to create them and use them as

4
00:00:07,120 --> 00:00:11,010
placeholders and do variable expansion, there are some advanced things,

5
00:00:11,010 --> 00:00:15,010
at least advanced from you as a beginner in PowerShell that

6
00:00:15,010 --> 00:00:17,940
you might want to work with variables.

7
00:00:17,940 --> 00:00:21,080
And these are things such as Tee‑Object,

8
00:00:21,080 --> 00:00:22,000
which is a cmdlet,

9
00:00:22,000 --> 00:00:25,740
which I'll show that allows you to basically pipe an

10
00:00:25,740 --> 00:00:30,840
expression to the pipeline and also to a variable.

11
00:00:30,840 --> 00:00:34,660
We're going to look in a moment here about OutVariable and then a more

12
00:00:34,660 --> 00:00:37,930
complex and advanced topic that I debated putting in,

13
00:00:37,930 --> 00:00:42,040
but I just want to show it to you because you may see this in examples,

14
00:00:42,040 --> 00:00:45,970
and I want you to at least have a rough understanding of how this works,

15
00:00:45,970 --> 00:00:48,640
and that is the PipelineVariable.

16
00:00:48,640 --> 00:00:52,900
So Tee‑Object, which has an alias of Tee, is pretty simple to use.

17
00:00:52,900 --> 00:00:56,920
You run any command you want on the left side of the pipe character, so I'm

18
00:00:56,920 --> 00:01:02,340
going to do Get‑Process ls*, and pipe that to Tee‑Object,

19
00:01:02,340 --> 00:01:07,150
and I'm going to assign the variable p to hold the results.

20
00:01:07,150 --> 00:01:11,400
So what's going to happen is if I run this in a PowerShell session,

21
00:01:11,400 --> 00:01:16,370
I will get the expression result, the process result will show on the screen,

22
00:01:16,370 --> 00:01:19,440
and I'll also have it saved to a variable,

23
00:01:19,440 --> 00:01:20,740
$p.

24
00:01:20,740 --> 00:01:23,290
So what will happen is I can run Get‑Process,

25
00:01:23,290 --> 00:01:27,080
see the results, and save the results to a variable.

26
00:01:27,080 --> 00:01:29,040
It looks like this.

27
00:01:29,040 --> 00:01:31,940
I get my processes that start with Ls,

28
00:01:31,940 --> 00:01:34,370
and the results have been saved to a variable,

29
00:01:34,370 --> 00:01:35,740
$p.

30
00:01:35,740 --> 00:01:39,870
So I can take $p, it's the placeholder that holds the results, and say,

31
00:01:39,870 --> 00:01:40,150
hey,

32
00:01:40,150 --> 00:01:43,640
I want to measure say the working set and get the sum of

33
00:01:43,640 --> 00:01:46,130
those ls processes. Pretty simple.

34
00:01:46,130 --> 00:01:50,210
So using Tee‑Object is a nice way to get the results and also

35
00:01:50,210 --> 00:01:53,060
save them to a variable all in one command.

36
00:01:53,060 --> 00:01:57,640
Now kind of related to Tee‑Object is OutVariable.

37
00:01:57,640 --> 00:02:00,510
OutVariable is a common parameter,

38
00:02:00,510 --> 00:02:04,080
which means all PowerShell commands and advanced

39
00:02:04,080 --> 00:02:06,600
functions will include ‑outvariable.

40
00:02:06,600 --> 00:02:08,130
In this situation,

41
00:02:08,130 --> 00:02:13,640
what's happening is I'm using $p that I had from my Tee‑Object example,

42
00:02:13,640 --> 00:02:15,880
and then I'm going to pipe this again to measure‑object.

43
00:02:15,880 --> 00:02:21,940
But this time, I'm going to save the results also to a variable, m.

44
00:02:21,940 --> 00:02:25,480
So the only thing that goes to $m will be the results

45
00:02:25,480 --> 00:02:27,940
of that measure‑object command.

46
00:02:27,940 --> 00:02:32,350
This is a nice way of saving output from an individual pipeline segment.

47
00:02:32,350 --> 00:02:34,770
It comes in very handy when you're troubleshooting or

48
00:02:34,770 --> 00:02:38,040
debugging why a command does not work.

49
00:02:38,040 --> 00:02:43,410
So I can use $m and say show me the sum property of that value.

50
00:02:43,410 --> 00:02:47,700
Now I could've also used Tee‑Object, but I wanted to demonstrate OutVariable.

51
00:02:47,700 --> 00:02:51,320
This is a parameter I use often in my PowerShell work and

52
00:02:51,320 --> 00:02:53,730
something I think you'll use as well.

53
00:02:53,730 --> 00:02:59,530
And there you can see, I have the value of the sum property of the variable $m,

54
00:02:59,530 --> 00:03:01,380
which is the measurement object.

55
00:03:01,380 --> 00:03:03,550
I hope you can kind of follow all of that.

56
00:03:03,550 --> 00:03:07,150
The last item related to variables is another common

57
00:03:07,150 --> 00:03:11,040
parameter called the PipelineVariable.

58
00:03:11,040 --> 00:03:13,680
Now this is admittedly an advanced concept,

59
00:03:13,680 --> 00:03:16,970
so don't feel that this makes your head hurt that you're

60
00:03:16,970 --> 00:03:19,270
doing something wrong or you're not up to it.

61
00:03:19,270 --> 00:03:21,750
I rarely use PipelineVariable.

62
00:03:21,750 --> 00:03:27,810
This allows you to save pipeline segment output across the pipeline and

63
00:03:27,810 --> 00:03:32,450
then reuse it in another part of the pipeline expression.

64
00:03:32,450 --> 00:03:33,940
Sounds confusing.

65
00:03:33,940 --> 00:03:35,040
It is confusing.

66
00:03:35,040 --> 00:03:36,300
Don't worry too much about it,

67
00:03:36,300 --> 00:03:40,640
but at least I want you to see what is happening here.

68
00:03:40,640 --> 00:03:43,390
What we're getting is a temporary, in essence,

69
00:03:43,390 --> 00:03:44,700
in‑memory variable,

70
00:03:44,700 --> 00:03:47,740
so I only kind of walk through what's happening here on the screen.

71
00:03:47,740 --> 00:03:50,200
So I'm getting the numbers 1 to 5.

72
00:03:50,200 --> 00:03:53,240
So that's the range operator, the dot dot.

73
00:03:53,240 --> 00:03:59,640
For each of those numbers, it's going to foreach‑object.

74
00:03:59,640 --> 00:04:04,410
And foreach‑object I'm just displaying the value 12345.

75
00:04:04,410 --> 00:04:09,840
That's in the parentheses, I'm sorry, that's in the curly braces and the {$_}.

76
00:04:09,840 --> 00:04:10,540
However,

77
00:04:10,540 --> 00:04:19,140
I'm also saving the output of foreach‑object as an in‑memory variable to $a,

78
00:04:19,140 --> 00:04:21,540
‑pipelinevariable a.

79
00:04:21,540 --> 00:04:23,740
So now I'm piping the numbers 1 through 5.

80
00:04:23,740 --> 00:04:28,030
I then pipe those results to another foreach‑object and

81
00:04:28,030 --> 00:04:32,740
save that variable to pipeline b.

82
00:04:32,740 --> 00:04:36,740
And this pipeline expression is taking the number 1

83
00:04:36,740 --> 00:04:39,840
through 5 and then multiplying it by 2.

84
00:04:39,840 --> 00:04:41,660
And then at the very end,

85
00:04:41,660 --> 00:04:45,420
I'm piping those results to another foreach‑object where

86
00:04:45,420 --> 00:04:48,870
I'm just displaying { "$a * 2 = $b"}.

87
00:04:48,870 --> 00:04:53,640
Now, in this case, I'm using that variable expansion to create a string.

88
00:04:53,640 --> 00:04:56,450
I admit this is an artificial example,

89
00:04:56,450 --> 00:04:59,770
but hopefully you can kind of see what is going on here.

90
00:04:59,770 --> 00:05:00,870
Let's look at the results.

91
00:05:00,870 --> 00:05:06,640
So $a is 12345, and that's as it comes through the pipeline,

92
00:05:06,640 --> 00:05:12,340
$b is then the results of $a, or basically,

93
00:05:12,340 --> 00:05:13,680
the first number times 2.

94
00:05:13,680 --> 00:05:16,080
It gets a little complicated.

95
00:05:16,080 --> 00:05:18,990
Again, this is for special use case scenarios.

96
00:05:18,990 --> 00:05:22,980
It's an advanced topic you may not use all the time.

97
00:05:22,980 --> 00:05:25,100
There are often other ways to get the job done

98
00:05:25,100 --> 00:05:31,000
without resorting to PipelineVariable, but if you need to use it, it is there.

