Writing to the Void

Writing to the void
Photo by Redvers

One thing that always bothered me about the normal Windows command line was the lack of an equivalent to /dev/null in UNIX. When running a command whose output you wanted to ignore (i.e. not have it cluttering up your console window) you had to redirect it to a real file. This always seemed a bit sloppy to me, because you ended up with file laying around containing who knows what that you really just wanted to ignore.

PowerShell, however, does have an equivalent. You can redirect to the built-in variable $null to get the same effect.

PS C:\> dir > $null

In addition to the $null variable you can also pipe output to the out-null cmdlet.

PS C:\> dir | out-null

This second version also has a little trick up its sleeve. Normally when running a GUI app from PowerShell, control is returned immediately to the command line and PowerShell doesn’t wait for the GUI app to exit. This may not be what you want. For example, in my build scripts I have a program that generates the documentation, it’s actually a GUI app that takes parameters and writes out the help files. I want my script to wait for this process to finish, so the normal behavior doesn’t work for me. But, by piping the output of the GUI app to out-null then PowerShell will wait for the app to finish. You can see this yourself by running notepad.

PS C:\> notepad | out-null