If you display large text files or long results of a cmdlet, the content rushes unabated through the console window. To display the output one page at a time, UNIX pagers such as more or less have become well established. PowerShell offers comparable tools, although they are not as powerful as their UNIX counterparts.

Wolfgang Sommergut

Wolfgang Sommergut has over 20 years of experience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication WindowsPro.de.

On modern operating systems, text-oriented shells usually run in the window of a graphical user interface. Thus, the program output isn’t lost after it disappeared from the visible area. You can scroll back through the content as long as you don’t exceed the window buffer. However, because this procedure is not really comfortable, we still require the established pagers.

Simple more command ^

Just like the old command-line interpreter, PowerShell offers a more command. Its usage is as simple as its functionality. You can pass either the contents of a file or the output of a cmdlet through a pipe to the more command. You can also use the -Paths parameter to specify the files that you want to display.

This example displays all XML files in the current folder, one page at a time. As you can see, you can also work with wildcards. The command is equivalent to the following example:

This usage is mostly identical to the more command in cmd.exe. The space bar allows you to page through the output, and the Enter key forwards the text by one line. “Q” quits the command.

A major downside of more is that it only starts with displaying data after the previous command has processed the entire contents. For instance, if you want to page through a huge log file, the following command would first read all the events before you would see the entries:

Out-Host as a performant alternative ^

With Out-Host, PowerShell has a second pager that is a bit smarter in this respect because it begins with displaying the content before it reached the end of the file. To make the cmdlet display the text page by page, you have to add the parameter -Paging:

To save typing, you can use the predefined alias oh for Out-Host:

Searching in the paged output

“Q” causes an uncaught exception in Out-Host

Usage and function of Out-Host are identical to more. The only difference is that the cmdlet shows the key mappings on the lower screen. Entering “Q” causes an uncaught exception, which is a little confusing.

No backward paging, no search ^

In comparison to the venerable less command in UNIX, both PowerShell pagers are a bit simple. They don’t support backward paging and don’t allow you to search in the displayed content. As a workaround for the latter, you could use the search function of the console program by navigating to Edit -> Find in the context menu of the title bar.

Searching in the paged output

Searching in the paged output

Otherwise, you only have the option to filter the content with Select-String before you pass it to Out-Host and more.

Get-Content, Select: variations of head and tail ^

If you have to deal with large amounts of text, you rarely want to page through the entire content. Sometimes you need to display only a certain number of lines at the beginning or the end of the output.

In the UNIX world, the tools head and tail serve this purpose. In PowerShell, Get-Content takes over this task. The parameters -First and -Last accept numerical values for the number of lines. The following example uses the alias gc for Get-Content:

The big downside of Get-Content is that the cmdlet doesn’t support input through the pipeline. For instance, you can’t use it to display the last 50 lines of the EventLog. Fortunately, Get-Eventlog offers -Newest, a corresponding parameter for the task. However, in other cases, such alternatives usually don’t exist.

Another option is the Select-Object cmdlet that also accepts the parameters -First and -Last. The weakness of the cmdlet is that, as with more, it has to wait until the entire input has been sent through the pipeline before it can extract the desired number of lines.

Win the monthly 4sysops member prize for IT pros

Share
2+

Users who have LIKED this post:

  • avatar

Related Posts

2 Comments
  1. Aleksey 2 years ago

    It's a pitty that "more" and out-host -paging isn't working in ISE.

    2+

    Users who have LIKED this comment:

    • avatar
    • hunter 1 month ago

      paging works in command prompt when you open cmd and type powershell the prompt becomes "PS $PWD." To use it pipe your command to output-host -paging.  Ex:

      get-wmiobject -list |out-host -paging

      get-wmiobject -list |oh -paging #For less typing

      0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account