Posey's Tips & Tricks

How To Use PowerShell's Out Verb

Your screen doesn't have to be PowerShell's only output device. As Brien shows, the Out verb lets you redirect PowerShell's output in a variety of useful ways.

Based on some of the e-mails that I have received and conversations that I have had, I think that most IT pros tend to think of PowerShell as strictly being a command-line environment. The administrator enters a command or runs a script, and PowerShell does the requested task and displays the results onscreen.

However, PowerShell is not limited to using the screen as its only output device. The Out verb gives you a number of options for redirecting PowerShell output.

As you probably know, PowerShell cmdlets are made up of verb-noun combinations. There may be several different verbs that work with a given noun. In Hyper-V, for instance, there are a number of virtual machine-related cmdlets such as Get-VM, Start-VM or New-VM. Similarly, verbs such as Out often work with several nouns.

If you want to see the nouns that the Out verb works with, just type Get-Command Out-*. This will cause PowerShell to display output similar to what you see in Figure 1.

[Click on image for larger view.] Figure 1: There are a number of Out commands.

The Out verb is designed to redirect PowerShell's output. As you can see from the commands listed in the screen capture above, it is possible to output data to a file, a host, a printer, a string or even a null (among other things).

So let's go ahead and try out a few of these commands. Suppose for a moment that we want to run the Get-VM command (which displays a list of the Hyper-V VMs residing on the machine) and redirect the output to a text file. To do so, you would use this command:

Get-VM |  Out-File C:\Data\VMs.txt

If we want to read the text file from within PowerShell, we can use this command:

Get-Content  -Path C:\Data\VMs.txt

You can see what these commands do in Figure 2.

[Click on image for larger view.] Figure 2: PowerShell output has been written to a text file.

As you can see, a command's output that would have ordinarily been displayed on the screen has been written to a text file instead. Incidentally, PowerShell also has the ability to output content to an HTML file using the ConvertTo-HTML command.

Redirecting PowerShell output to a file definitely has its uses, but what about the Out-Host option that I mentioned earlier? This one is a little bit tricky. The Out-Host cmdlet tells PowerShell to display its output on the screen, which it already does by default. So why would anyone use the Out-Host cmdlet?

The reason why the Out-Host cmdlet may be useful is because it can be used to break a command's output into pages so that the text doesn't go scrolling off the screen.

Suppose that I wanted to look at a list of system services. If I just type Get-Service, the output will quickly scroll off the screen. If I instead type Get-Service | Out-Host, then the command will behave exactly as it did before. Remember, PowerShell is already redirecting output to the host (to the screen). If, however, you append the -Paging parameter to the command, then the output will be divided into pages. Figure 3 shows what the output looks like.

Figure 3: The last line of text indicates that you can press Space to look at the next page, or press enter to see the next line.

The Out verb can also output data to a printer. All you have to do is to use the Get-Printer command to get a list of the printers that are attached to the system. From there, simply append the printer name to the Out-Printer cmdlet to redirect output to the printer. Figure 4 shows an example of this command.

[Click on image for larger view.] Figure 4: PowerShell can redirect its output to a printer.

One last thing that I want to mention is the Out-Null cmdlet. Most people will probably never have to use this one, but it does have its place. If you have ever written a PowerShell script and had to deal with unwanted output on the screen, then this cmdlet is for you. Out-Null suppresses another command's output, making it so that the output is never displayed onscreen. If I wanted to hide the list of services, for example, I might type Get-Service | Out-Null.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

comments powered by Disqus

Subscribe on YouTube

Upcoming Training Events

0 AM
TechMentor @ Microsoft HQ
August 11-15, 2025