We've been so far working without cmdlets that produce
something at the end, whether that be a file or a printed
document or something on the screen.
Out‑Null is the only out cmdlet that actually doesn't give us any output.
Let's jump in and explore.
Let's launch the help with help out‑null. So Out‑Null is
responsible for hiding the output of a command instead of
sending it down the pipeline or displaying it.
There's only one parameter here,
which is InputObject, being the object that we want to work with.
And the information that's here in this description is
really important to let sink in.
So the Out‑Null cmdlet sends output to NULL, which, in
effect, removes it from the pipeline and prevents it from
being displayed to the screen.
So do be aware of exactly what you're doing when you use
Out‑Null. Why might you want to use this?
Well, let's take a look at an example.
I'll use the New‑Item command to create an item called test,
and I'll use the ItemType parameter to configure that as a
directory. That will create a new folder in the C:4 drive called
test. And by default, the cmdlet New‑Item outputs all of this
information to the screen when it gets run.
Now, I'll get rid of that test folder by using Remove‑Item, and
let's rerun the New‑Item cmdlet, but this time we'll pipe it across
to Out‑Null. This time we get nothing returned to us on screen, but
if I do a Get‑ChildItem, you can see here that the test folder was
still created, so the cmdlet did run successfully.
It just didn't send its normal output to the screen.
When might you actually use this?
Well,
some common scenarios or when you simply just want to suppress output that's
sent to the PowerShell console like we did a moment ago.
There are also times when you might be writing a function or a script,
and you might be creating something that's reusable by other people like
a module that's published to the PowerShell gallery.
And some of those commands that you end up using in your own
functions and modules could end up returning some unwanted
results like this that really aren't useful,
so you may just want to silence that output.
We briefly saw this in the help at the start.
But if I use New‑Item again to try and create a directory and
send that to out‑null, at this point that would work just fine,
but let's pipe that down to Get‑Member.
And, yeah,
PowerShell gives us an error because out‑null is actually
removing that object from the pipeline entirely.
So nothing is being sent across the pipeline to Get‑Member.
Something to be aware of is that Out‑Null,
by default, is responsible for dealing with these success stream,
which is stream number 1 in PowerShell.
There are other streams that are used for messages like errors,
warnings, and for both.
So if I run a command that I know will fail like Get‑ChildItem
potato and then pipe that across to Out‑Null,
we will still get the error message shown on screen because
Out‑Null is only suppressing the success stream.
It's not working with any of the other streams in PowerShell.
Let's take a look at a bit of a nuance here. So I'm going to
create a new variable named $dir, and in that we'll do
Get‑ChildItem and pipe that to Out‑Null. We get no output as
expected, but if I run $dir on its own,
you might've expected to see the results of Get‑ChildItem to be shown on screen.
What you can do is enclose the first section in parentheses,
though now if we run $dir, we get the results of that Get‑ChildItem command.
Lastly, while we have been looking specifically at Out‑Null in this clip,
I want to expand on that a little bit and let you know about some other ways to
null the output of commands because there's several popular ways to do it, and
some of the other ways do have some speed benefits,
too.
To start with, I want to create a variable,
and I'm going to set it as a New‑Object with the type
of System.Collections.ArrayList.
So this will allow me to store items here as an array. If I
type $MyArray and then use the Add operator, I can specify a
string of text to add into the array,
so I'm going to call this Thing1. Notice here that by default this returned an
output of 0. Not exactly helpful is it? Let me do this again, and this time I'll
add Thing2. And this time we get an output of 1.
And just for good luck,
one more time for Thing3, and this time we get an output of 2.
The output that's being displayed there is what number the
object is in the array that we're adding to.
But for my purposes,
the output really isn't needed nor helpful, so let's suppress it.
I'll run it again for Thing4, but this time pipe it to Out‑Null.
Okay, no output this time, which is good.
And if I check the values in the variable,
we've got Thing1 through Thing4. Awesome.
Let's check out a few other ways to null output.
I'll run the same command to add Thing5 into the array, but this time I'm going
to use one of the redirection operators that we looked at back in the out file
clip, and I'm going to redirect it to $null. In PowerShell, $null is an
automatic variable used to represent null,
and it will treat $null as an object with a value of null.
So again, no output was seen on the screen.
And if we check the variable again, Thing5 has been added in.
We could also make use of $null at the start of the command and then
put an equal sign and then run our command. Again,
no output.
And if we take a look at the variable, Thing6 has been added in.
And lastly,
this one is more of a raw programming way to achieve the same
thing, but you can cast the command to void by using the square
brackets and the word [void], followed by the command that you are
wanting to suppress the output on. Again, we don't have that output
here in the console.
And if we check that variable one more time,
Thing7 absolutely got added in there at the end.
There were a couple of reasons I wanted to show you these extra ways of
working with null other than just the Out‑Null command.
The first one really was just for completeness so you're
aware of them and that you can use them.
You'll often see examples of these being used,
especially the redirection to $null and $null =. But also, most,
if not all,
of these methods on screen are actually faster than
using the Out‑Null cmdlet as well.
If you're processing a lot of information and want to suppress the output,
make sure you use the cmdlet called measure command and try a few different
ways to see which one is going to be most performant for you.
These examples on screen for the most part avoid the pipeline, which can add some additional processing time to your cmdlets.