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
2009-07-03 11:34:01
thanks, corrected the question
Valentin Vasiliev
2009-07-03 11:40:08
+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
2009-07-03 12:00:57
more does exactly that, when you take a look at the definition of the function with gcm more|select Definition|fl
Joey
2009-07-03 12:37:08
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
2009-07-03 12:50:24
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
2009-07-03 13:23:58
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
2009-07-03 14:15:57
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
2009-07-03 14:17:29
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
2009-07-04 00:59:38
+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
2009-07-03 16:45:40
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
2010-06-17 20:32:22
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
2010-09-15 08:24:01