1
00:00:00,940 --> 00:00:04,990
The first Out cmdlet we're going to explore is a pretty popular one,

2
00:00:04,990 --> 00:00:07,040
which is Out‑File.

3
00:00:07,040 --> 00:00:10,040
The name of this cmdlet is pretty self‑explanatory,

4
00:00:10,040 --> 00:00:14,240
but let's check out the help by running help Out‑File.

5
00:00:14,240 --> 00:00:16,240
The synopsis is straightforward enough.

6
00:00:16,240 --> 00:00:18,940
It sends output to a file.

7
00:00:18,940 --> 00:00:21,960
Something of interest here is that it doesn't say what

8
00:00:21,960 --> 00:00:24,400
type of file you need to specify.

9
00:00:24,400 --> 00:00:27,140
And that's because Out‑File is dealing with text,

10
00:00:27,140 --> 00:00:30,430
and it will output to a file with whatever extension you give it.

11
00:00:30,430 --> 00:00:34,240
But in our demo, we're going to work with a plain old text file.

12
00:00:34,240 --> 00:00:38,680
Let's keep using the example from the last module of working with Get‑Process.

13
00:00:38,680 --> 00:00:41,890
And I'm going to just choose processes starting with an s so we

14
00:00:41,890 --> 00:00:44,220
don't have a lot of data to scroll around with.

15
00:00:44,220 --> 00:00:47,390
We can simply pipe that across to Out‑File,

16
00:00:47,390 --> 00:00:49,630
which has a parameter named ‑FilePath,

17
00:00:49,630 --> 00:00:52,080
which is where you need to tell PowerShell where you

18
00:00:52,080 --> 00:00:54,640
want the data to be output to.

19
00:00:54,640 --> 00:00:59,840
I'll store it in C:\Docs\ and call the file process.txt.

20
00:00:59,840 --> 00:01:03,090
I could show this to you in the console with Get‑Content,

21
00:01:03,090 --> 00:01:07,590
but I want to open this up in Notepad just so you can visualize what's going on.

22
00:01:07,590 --> 00:01:13,240
So I'll run notepad C:\Docs\process.txt.

23
00:01:13,240 --> 00:01:17,240
Let me make that bigger, and there are a couple of things to point out here.

24
00:01:17,240 --> 00:01:19,780
Firstly, this is just a raw text file.

25
00:01:19,780 --> 00:01:22,180
These are no longer PowerShell objects.

26
00:01:22,180 --> 00:01:25,380
I can't import this back in and suddenly have PowerShell

27
00:01:25,380 --> 00:01:27,940
objects at my disposal to work with.

28
00:01:27,940 --> 00:01:30,770
Secondly, I didn't specify a format command,

29
00:01:30,770 --> 00:01:34,430
so PowerShell has used the default formatting for Get‑Process,

30
00:01:34,430 --> 00:01:38,940
which is to output the results to a table with these specific properties.

31
00:01:38,940 --> 00:01:40,090
With that said,

32
00:01:40,090 --> 00:01:44,640
you can override the formatting before you output the data to a file.

33
00:01:44,640 --> 00:01:49,050
So if I run Get‑Process s*, I can pipe that across to,

34
00:01:49,050 --> 00:01:52,830
say, Format‑List and then pipe that down to Out‑File.

35
00:01:52,830 --> 00:01:57,710
The ‑FilePath parameter is positional, so I don't need to specify the parameter,

36
00:01:57,710 --> 00:02:00,330
and I can just type the path of the file.

37
00:02:00,330 --> 00:02:05,640
And I'll use the same one, being C:\Docs\process.txt.

38
00:02:05,640 --> 00:02:08,150
I'll up arrow to get the Notepad command again.

39
00:02:08,150 --> 00:02:13,040
And this time you can see we've got the list format in the external text file.

40
00:02:13,040 --> 00:02:15,200
Notice that I used the same file name,

41
00:02:15,200 --> 00:02:18,240
and it didn't ask me if I want to overwrite the file.

42
00:02:18,240 --> 00:02:19,680
That's the default behavior.

43
00:02:19,680 --> 00:02:21,660
And if you want to stop that from happening,

44
00:02:21,660 --> 00:02:24,800
you can use this ‑NoClobber parameter to prevent an

45
00:02:24,800 --> 00:02:27,230
existing file from being overwritten.

46
00:02:27,230 --> 00:02:31,340
And we'll get an error to say that this file already exists.

47
00:02:31,340 --> 00:02:33,670
Out‑File has an ‑Append parameter.

48
00:02:33,670 --> 00:02:37,460
So notice I've taken the Format‑List command out of the pipeline,

49
00:02:37,460 --> 00:02:40,340
but I'm using the ‑Append parameter.

50
00:02:40,340 --> 00:02:42,550
I'll open up the file again in Notepad,

51
00:02:42,550 --> 00:02:46,190
and we've got our original list format from a few commands ago.

52
00:02:46,190 --> 00:02:49,240
And if I scroll down towards the bottom here,

53
00:02:49,240 --> 00:02:51,680
we'll see the table output has been appended to the

54
00:02:51,680 --> 00:02:55,240
same file from the cmdlet I just ran.

55
00:02:55,240 --> 00:02:58,890
The parameter named ‑NoNewLine will ensure the content written to

56
00:02:58,890 --> 00:03:02,140
the file doesn't end with a new line character.

57
00:03:02,140 --> 00:03:05,770
The string representations of the object being specified

58
00:03:05,770 --> 00:03:08,580
are concatenated to form the output.

59
00:03:08,580 --> 00:03:10,810
Notepad is doing a bit of wrapping here,

60
00:03:10,810 --> 00:03:14,740
but this entire output is actually on a single line.

61
00:03:14,740 --> 00:03:16,880
Another parameter is named ‑Width,

62
00:03:16,880 --> 00:03:20,100
which is where you can tell PowerShell how many characters

63
00:03:20,100 --> 00:03:22,940
should be present on each line of the output.

64
00:03:22,940 --> 00:03:27,040
Any additional characters are truncated; they aren't wrapped.

65
00:03:27,040 --> 00:03:31,440
Let's set that width to 20 and then open the file in Notepad.

66
00:03:31,440 --> 00:03:36,540
And we've only got the first 20 characters of each line in the default output.

67
00:03:36,540 --> 00:03:40,130
The Out cmdlets all have a parameter named ‑InputObject,

68
00:03:40,130 --> 00:03:44,070
so in the examples above, the data was being piped to Out‑File,

69
00:03:44,070 --> 00:03:49,940
but you can use the ‑InputObject parameter and specify the data to be output.

70
00:03:49,940 --> 00:03:52,460
So let's create a variable named process,

71
00:03:52,460 --> 00:03:56,140
and we'll use that to store the processes starting with s.

72
00:03:56,140 --> 00:03:59,240
You can then just call Out‑File on its own and use the

73
00:03:59,240 --> 00:04:02,600
‑InputObject parameter to specify the variable where the

74
00:04:02,600 --> 00:04:05,090
objects are stored that you want to output,

75
00:04:05,090 --> 00:04:09,640
and then specify the ‑FilePath for the file to output to.

76
00:04:09,640 --> 00:04:12,220
If we check that with Notepad, functionally,

77
00:04:12,220 --> 00:04:14,920
this is the same as piping the data to Out‑File.

78
00:04:14,920 --> 00:04:18,940
But it's just another way of specifying the data to be output.

79
00:04:18,940 --> 00:04:20,250
In the previous clip,

80
00:04:20,250 --> 00:04:24,840
I mentioned that you can't pipe Out cmdlets to another cmdlet because

81
00:04:24,840 --> 00:04:27,480
Out cmdlets remove the objects from the pipeline,

82
00:04:27,480 --> 00:04:30,940
and they send the data out of the PowerShell system.

83
00:04:30,940 --> 00:04:33,520
Out‑File saves the data to a file,

84
00:04:33,520 --> 00:04:37,440
but it does not produce any output objects to the pipeline.

85
00:04:37,440 --> 00:04:42,480
Let me get the processes starting with s again and pop that across to Out‑File.

86
00:04:42,480 --> 00:04:44,850
And then we can pipe that down to Get‑Member.

87
00:04:44,850 --> 00:04:47,180
Get‑Member errors out here,

88
00:04:47,180 --> 00:04:50,720
telling us that we must specify an object for the Get‑Member cmdlet.

89
00:04:50,720 --> 00:04:55,540
So this shows that nothing is being sent down the pipeline from Out‑File.

90
00:04:55,540 --> 00:04:57,820
There's something else I want to cover here briefly,

91
00:04:57,820 --> 00:05:01,210
and it isn't specifically related to Out‑File,

92
00:05:01,210 --> 00:05:05,190
but it is related to getting output from PowerShell to text files.

93
00:05:05,190 --> 00:05:08,760
And you might be familiar with this from command.exe days.

94
00:05:08,760 --> 00:05:11,730
And that is using the redirection operators to get

95
00:05:11,730 --> 00:05:14,940
data from PowerShell out to a file.

96
00:05:14,940 --> 00:05:19,960
I want to show you the help file named about_redirection,

97
00:05:19,960 --> 00:05:23,440
and there are a couple of things I'd like to highlight in here.

98
00:05:23,440 --> 00:05:27,320
As we've learned, by default, PowerShell sends its output to the console,

99
00:05:27,320 --> 00:05:31,240
but we can direct the output to a text file instead.

100
00:05:31,240 --> 00:05:35,060
There are these things called streams, which we'll see more about in a moment.

101
00:05:35,060 --> 00:05:36,470
But, interestingly,

102
00:05:36,470 --> 00:05:41,740
you can also redirect the error output to the regular output stream.

103
00:05:41,740 --> 00:05:45,200
And over here, it lists a few ways to redirect output,

104
00:05:45,200 --> 00:05:48,830
the first being to use Out‑File, which is what we've worked through here.

105
00:05:48,830 --> 00:05:50,190
And in this help document,

106
00:05:50,190 --> 00:05:55,440
they note that you'd typically use Out‑File when you want to use its parameters.

107
00:05:55,440 --> 00:05:57,960
There's another cmdlet named Tee‑Object.

108
00:05:57,960 --> 00:06:01,940
And, lastly, you can use these things called redirection operators.

109
00:06:01,940 --> 00:06:05,340
If I press space to page on to the next screen in the help document,

110
00:06:05,340 --> 00:06:09,040
we get some information about the redirection operators.

111
00:06:09,040 --> 00:06:11,040
I'm not going to go through all of this with you.

112
00:06:11,040 --> 00:06:13,710
You're obviously welcome to do that in your own time.

113
00:06:13,710 --> 00:06:17,250
But a couple of things to highlight here are in the middle,

114
00:06:17,250 --> 00:06:19,780
and these are all of the output streams available in

115
00:06:19,780 --> 00:06:22,780
PowerShell to output different types of data.

116
00:06:22,780 --> 00:06:25,530
And the default stream, if you don't specify one,

117
00:06:25,530 --> 00:06:28,130
is Stream #1, the SUCCESS Stream.

118
00:06:28,130 --> 00:06:33,140
At the very bottom, it starts to tell us what the redirection operators are,

119
00:06:33,140 --> 00:06:36,490
and this first one that's on screen, you may be familiar with,

120
00:06:36,490 --> 00:06:38,180
which is the greater‑than sign.

121
00:06:38,180 --> 00:06:41,940
And that sends the specified stream to a file.

122
00:06:41,940 --> 00:06:45,040
I'll page down here just once more, and at the very top,

123
00:06:45,040 --> 00:06:48,080
there's another operator, which is two greater‑than signs,

124
00:06:48,080 --> 00:06:51,740
which will append the specified stream to a file.

125
00:06:51,740 --> 00:06:54,890
And this last syntax here allows you to redirect one of

126
00:06:54,890 --> 00:06:58,040
the other streams to the success stream.

127
00:06:58,040 --> 00:07:00,840
Let's do a quick example of that in the console.

128
00:07:00,840 --> 00:07:02,470
If we run Get‑Process again,

129
00:07:02,470 --> 00:07:06,710
we can just then use the greater‑than symbol as the redirection operator

130
00:07:06,710 --> 00:07:10,240
and then specify the name of the file to output to.

131
00:07:10,240 --> 00:07:12,040
I'll fire that up in Notepad.

132
00:07:12,040 --> 00:07:12,910
And, sure enough,

133
00:07:12,910 --> 00:07:15,620
we get the same output that we were getting with Out‑File

134
00:07:15,620 --> 00:07:18,170
because it's effectively doing the same thing.

135
00:07:18,170 --> 00:07:21,710
Back in the console, if we bring up that same command,

136
00:07:21,710 --> 00:07:24,370
but I'll go back to add another greater‑than symbol,

137
00:07:24,370 --> 00:07:27,840
this is telling PowerShell to append the data to the file.

138
00:07:27,840 --> 00:07:32,740
So this is exactly the same as using Out‑File with the ‑Append parameter.

139
00:07:32,740 --> 00:07:35,080
Depending on what you're doing in the shell,

140
00:07:35,080 --> 00:07:37,800
you might find yourself using the redirection operator,

141
00:07:37,800 --> 00:07:44,000
but Out‑File definitely can be useful if you need some extra controls around the output.

