tags:

views:

916

answers:

4

Is there a way to paginate the output by piping it to some 'more' command, which is available in linux\unix shells?

+1  A: 

more isn't used to limit output, it's used to paginate output and make it easier to read in a terminal, if anything.

Are you talking about using head and tail? EggHeadCafe has an example of:

type my.txt | select-object -first 10

type my.txt | select-object -last 10

to emulate head and tail.

Mark Rushakoff
thanks, corrected the question
Valentin Vasiliev
+6  A: 

Well... There is "more", which is more or less (...) the same you'd expect from other platforms. Try the following example:

dir -rec | more
Jouni Heikniemi
more does exactly that, when you take a look at the definition of the function with gcm more|select Definition|fl
Joey
Actually, piping to more is equal to piping to "out-host -paging", which is separate from the more.com implementation. In practice there is no discernable difference though.
Jouni Heikniemi
Neither in PS1 on Vista nor PS2 on Win7, at least for me. "more" is a function which does a little work beforehand and then just calls more.com. Out-Host -Paging is separate from that (and doesn't depend on more.com). The difference is most noticeable when you use something that doesn't return right away, such as your gci -rec example. Piping to Out-Host is immediate, as it can process the input one line at a time, piping to more takes time as PS first collects all lines and then sends them to more.com.
Joey
Interesting! For me, gcm more returns two definitions, one is a PowerShell function that, when given an empty argument, does "$input | out-host -p", which is the behavior I'm seeing on gci -rec | more. On the other hand, when I do gci -rec | more.com, I get the normal more.com behavior.On W7 RC with PS2 installed, I also seem to get more.com even when typing just "more". On Vista with PS1, the behavior described above occurs. Based on http://huddledmasses.org/powershell-power-user-tips-get-command-precedence/, wouldn't you think the function should be executed on W7 also? Hmm...
Jouni Heikniemi
Sorry, that wasn't particularly well-written. So my point was that on Vista with PowerShell 1, piping to just "more" invokes Out-Host -p while on W7 with PowerShell 2 it invokes more.com.
Jouni Heikniemi
Eep, I stand corrected. Sorry. Yes, indeed, in PS 1 it invokes Out-Host -p, not more.com (note to self: Always read the whole function, even if it looks very similar)
Joey
+1  A: 

The Powershell Community Extensions have a handy function named 'less' that provides a more complete Unix-style feature set, using a ported copy of less.exe to actually handle the paging.

C:\Code> cat Function:\Less

$OutputEncoding = [Console]::OutputEncoding

if ($args) {
    & "$Pscx:Home\Applications\Less-394\less.exe" @($args |? { test-path $_ } |% { "`"$(resolve-path $_)`"" })
}
else {
    $input | out-string | & "$Pscx:Home\Applications\Less-394\less.exe"
}

You can pipe strings to it, or give filenames as direct parameters.

type foo.txt | less
less foo.txt, bar.txt, baz.txt

Unfortunately it doesn't work the way you'd expect under the v2.0 ISE.

Richard Berg
+4  A: 

Yes there is:

some-cmdlet | out-host -paging

Shay Levy
I had used more and less before, neither being quite as user friendly as I should think possible. I found this variation to be more useful/usable than the others. Thanks very much.
TheXenocide
For printing the content of a very large file this worked very nice for me as the Get-Content cmdlet started immedialety to pipe the contents to the out-host cmdlet.
Germán