Latest posts by Wolfgang Sommergut (see all)
- Create, sort and filter CSV files in PowerShell - Tue, Sep 5 2017
- PSEdit - Edit files remotely with PowerShell - Fri, Sep 1 2017
- No future for Windows PowerShell—change to PowerShell Core - Tue, Aug 29 2017
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:
Get-Content *.xml | more
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:
Get-EventLog Security | more
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:
Get-Help Add-ADGroupMember -Detailed | Out-Host –Paging
To save typing, you can use the predefined alias oh for Out-Host:
Get-EventLog Security | oh –Paging
“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
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:
gc GPORep.xml -Last 10
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.