1
00:00:01,040 --> 00:00:04,420
We've been so far working without cmdlets that produce

2
00:00:04,420 --> 00:00:07,780
something at the end, whether that be a file or a printed

3
00:00:07,780 --> 00:00:10,540
document or something on the screen.

4
00:00:10,540 --> 00:00:15,340
Out‑Null is the only out cmdlet that actually doesn't give us any output.

5
00:00:15,340 --> 00:00:17,440
Let's jump in and explore.

6
00:00:17,440 --> 00:00:22,190
Let's launch the help with help out‑null. So Out‑Null is

7
00:00:22,190 --> 00:00:25,900
responsible for hiding the output of a command instead of

8
00:00:25,900 --> 00:00:29,340
sending it down the pipeline or displaying it.

9
00:00:29,340 --> 00:00:31,010
There's only one parameter here,

10
00:00:31,010 --> 00:00:34,760
which is InputObject, being the object that we want to work with.

11
00:00:34,760 --> 00:00:37,010
And the information that's here in this description is

12
00:00:37,010 --> 00:00:39,240
really important to let sink in.

13
00:00:39,240 --> 00:00:42,990
So the Out‑Null cmdlet sends output to NULL, which, in

14
00:00:42,990 --> 00:00:46,840
effect, removes it from the pipeline and prevents it from

15
00:00:46,840 --> 00:00:48,640
being displayed to the screen.

16
00:00:48,640 --> 00:00:51,470
So do be aware of exactly what you're doing when you use

17
00:00:51,470 --> 00:00:54,560
Out‑Null. Why might you want to use this?

18
00:00:54,560 --> 00:00:57,140
Well, let's take a look at an example.

19
00:00:57,140 --> 00:01:01,080
I'll use the New‑Item command to create an item called test,

20
00:01:01,080 --> 00:01:04,030
and I'll use the ItemType parameter to configure that as a

21
00:01:04,030 --> 00:01:08,030
directory. That will create a new folder in the C:4 drive called

22
00:01:08,030 --> 00:01:12,240
test. And by default, the cmdlet New‑Item outputs all of this

23
00:01:12,240 --> 00:01:15,240
information to the screen when it gets run.

24
00:01:15,240 --> 00:01:19,140
Now, I'll get rid of that test folder by using Remove‑Item, and

25
00:01:19,140 --> 00:01:22,980
let's rerun the New‑Item cmdlet, but this time we'll pipe it across

26
00:01:22,980 --> 00:01:27,680
to Out‑Null. This time we get nothing returned to us on screen, but

27
00:01:27,680 --> 00:01:31,750
if I do a Get‑ChildItem, you can see here that the test folder was

28
00:01:31,750 --> 00:01:34,900
still created, so the cmdlet did run successfully.

29
00:01:34,900 --> 00:01:38,240
It just didn't send its normal output to the screen.

30
00:01:38,240 --> 00:01:40,010
When might you actually use this?

31
00:01:40,010 --> 00:01:40,810
Well,

32
00:01:40,810 --> 00:01:44,710
some common scenarios or when you simply just want to suppress output that's

33
00:01:44,710 --> 00:01:48,240
sent to the PowerShell console like we did a moment ago.

34
00:01:48,240 --> 00:01:51,950
There are also times when you might be writing a function or a script,

35
00:01:51,950 --> 00:01:55,450
and you might be creating something that's reusable by other people like

36
00:01:55,450 --> 00:01:58,050
a module that's published to the PowerShell gallery.

37
00:01:58,050 --> 00:02:00,530
And some of those commands that you end up using in your own

38
00:02:00,530 --> 00:02:03,840
functions and modules could end up returning some unwanted

39
00:02:03,840 --> 00:02:06,540
results like this that really aren't useful,

40
00:02:06,540 --> 00:02:09,140
so you may just want to silence that output.

41
00:02:09,140 --> 00:02:11,740
We briefly saw this in the help at the start.

42
00:02:11,740 --> 00:02:15,390
But if I use New‑Item again to try and create a directory and

43
00:02:15,390 --> 00:02:19,000
send that to out‑null, at this point that would work just fine,

44
00:02:19,000 --> 00:02:21,940
but let's pipe that down to Get‑Member.

45
00:02:21,940 --> 00:02:23,200
And, yeah,

46
00:02:23,200 --> 00:02:26,330
PowerShell gives us an error because out‑null is actually

47
00:02:26,330 --> 00:02:29,180
removing that object from the pipeline entirely.

48
00:02:29,180 --> 00:02:33,340
So nothing is being sent across the pipeline to Get‑Member.

49
00:02:33,340 --> 00:02:35,790
Something to be aware of is that Out‑Null,

50
00:02:35,790 --> 00:02:39,490
by default, is responsible for dealing with these success stream,

51
00:02:39,490 --> 00:02:42,440
which is stream number 1 in PowerShell.

52
00:02:42,440 --> 00:02:45,740
There are other streams that are used for messages like errors,

53
00:02:45,740 --> 00:02:47,540
warnings, and for both.

54
00:02:47,540 --> 00:02:51,720
So if I run a command that I know will fail like Get‑ChildItem

55
00:02:51,720 --> 00:02:55,010
potato and then pipe that across to Out‑Null,

56
00:02:55,010 --> 00:02:58,200
we will still get the error message shown on screen because

57
00:02:58,200 --> 00:03:01,140
Out‑Null is only suppressing the success stream.

58
00:03:01,140 --> 00:03:05,340
It's not working with any of the other streams in PowerShell.

59
00:03:05,340 --> 00:03:08,480
Let's take a look at a bit of a nuance here. So I'm going to

60
00:03:08,480 --> 00:03:12,880
create a new variable named $dir, and in that we'll do

61
00:03:12,880 --> 00:03:17,590
Get‑ChildItem and pipe that to Out‑Null. We get no output as

62
00:03:17,590 --> 00:03:21,540
expected, but if I run $dir on its own,

63
00:03:21,540 --> 00:03:26,440
you might've expected to see the results of Get‑ChildItem to be shown on screen.

64
00:03:26,440 --> 00:03:30,180
What you can do is enclose the first section in parentheses,

65
00:03:30,180 --> 00:03:36,340
though now if we run $dir, we get the results of that Get‑ChildItem command.

66
00:03:36,340 --> 00:03:41,040
Lastly, while we have been looking specifically at Out‑Null in this clip,

67
00:03:41,040 --> 00:03:44,760
I want to expand on that a little bit and let you know about some other ways to

68
00:03:44,760 --> 00:03:49,120
null the output of commands because there's several popular ways to do it, and

69
00:03:49,120 --> 00:03:51,360
some of the other ways do have some speed benefits,

70
00:03:51,360 --> 00:03:52,440
too.

71
00:03:52,440 --> 00:03:54,740
To start with, I want to create a variable,

72
00:03:54,740 --> 00:03:57,870
and I'm going to set it as a New‑Object with the type

73
00:03:57,870 --> 00:04:00,780
of System.Collections.ArrayList.

74
00:04:00,780 --> 00:04:04,820
So this will allow me to store items here as an array. If I

75
00:04:04,820 --> 00:04:09,700
type $MyArray and then use the Add operator, I can specify a

76
00:04:09,700 --> 00:04:11,860
string of text to add into the array,

77
00:04:11,860 --> 00:04:17,310
so I'm going to call this Thing1. Notice here that by default this returned an

78
00:04:17,310 --> 00:04:22,910
output of 0. Not exactly helpful is it? Let me do this again, and this time I'll

79
00:04:22,910 --> 00:04:26,440
add Thing2. And this time we get an output of 1.

80
00:04:26,440 --> 00:04:27,630
And just for good luck,

81
00:04:27,630 --> 00:04:32,240
one more time for Thing3, and this time we get an output of 2.

82
00:04:32,240 --> 00:04:34,850
The output that's being displayed there is what number the

83
00:04:34,850 --> 00:04:37,220
object is in the array that we're adding to.

84
00:04:37,220 --> 00:04:38,600
But for my purposes,

85
00:04:38,600 --> 00:04:43,040
the output really isn't needed nor helpful, so let's suppress it.

86
00:04:43,040 --> 00:04:47,540
I'll run it again for Thing4, but this time pipe it to Out‑Null.

87
00:04:47,540 --> 00:04:50,220
Okay, no output this time, which is good.

88
00:04:50,220 --> 00:04:52,340
And if I check the values in the variable,

89
00:04:52,340 --> 00:04:56,140
we've got Thing1 through Thing4. Awesome.

90
00:04:56,140 --> 00:04:59,140
Let's check out a few other ways to null output.

91
00:04:59,140 --> 00:05:03,710
I'll run the same command to add Thing5 into the array, but this time I'm going

92
00:05:03,710 --> 00:05:07,540
to use one of the redirection operators that we looked at back in the out file

93
00:05:07,540 --> 00:05:13,780
clip, and I'm going to redirect it to $null. In PowerShell, $null is an

94
00:05:13,780 --> 00:05:16,940
automatic variable used to represent null,

95
00:05:16,940 --> 00:05:21,440
and it will treat $null as an object with a value of null.

96
00:05:21,440 --> 00:05:24,090
So again, no output was seen on the screen.

97
00:05:24,090 --> 00:05:28,340
And if we check the variable again, Thing5 has been added in.

98
00:05:28,340 --> 00:05:32,510
We could also make use of $null at the start of the command and then

99
00:05:32,510 --> 00:05:36,680
put an equal sign and then run our command. Again,

100
00:05:36,680 --> 00:05:37,900
no output.

101
00:05:37,900 --> 00:05:42,040
And if we take a look at the variable, Thing6 has been added in.

102
00:05:42,040 --> 00:05:42,990
And lastly,

103
00:05:42,990 --> 00:05:46,470
this one is more of a raw programming way to achieve the same

104
00:05:46,470 --> 00:05:50,780
thing, but you can cast the command to void by using the square

105
00:05:50,780 --> 00:05:54,030
brackets and the word [void], followed by the command that you are

106
00:05:54,030 --> 00:05:58,100
wanting to suppress the output on. Again, we don't have that output

107
00:05:58,100 --> 00:05:59,220
here in the console.

108
00:05:59,220 --> 00:06:01,910
And if we check that variable one more time,

109
00:06:01,910 --> 00:06:05,840
Thing7 absolutely got added in there at the end.

110
00:06:05,840 --> 00:06:09,120
There were a couple of reasons I wanted to show you these extra ways of

111
00:06:09,120 --> 00:06:12,640
working with null other than just the Out‑Null command.

112
00:06:12,640 --> 00:06:15,370
The first one really was just for completeness so you're

113
00:06:15,370 --> 00:06:17,540
aware of them and that you can use them.

114
00:06:17,540 --> 00:06:19,910
You'll often see examples of these being used,

115
00:06:19,910 --> 00:06:26,840
especially the redirection to $null and $null =. But also, most,

116
00:06:26,840 --> 00:06:27,460
if not all,

117
00:06:27,460 --> 00:06:30,260
of these methods on screen are actually faster than

118
00:06:30,260 --> 00:06:32,840
using the Out‑Null cmdlet as well.

119
00:06:32,840 --> 00:06:36,570
If you're processing a lot of information and want to suppress the output,

120
00:06:36,570 --> 00:06:40,740
make sure you use the cmdlet called measure command and try a few different

121
00:06:40,740 --> 00:06:44,440
ways to see which one is going to be most performant for you.

122
00:06:44,440 --> 00:06:53,000
These examples on screen for the most part avoid the pipeline, which can add some additional processing time to your cmdlets.

