178

Is there a way to paginate output by piping it to a more or less command, similar to those that are available in Linux\Unix shells?

1
  • 10
    For those who don't know less, some cool things about it is that you can go backwards, use arrows, use page up/down and even search using the key '/' and have the results highlighted. Commented Mar 8, 2020 at 11:47

15 Answers 15

163

Yes there is:

some-cmdlet | out-host -paging

Sign up to request clarification or add additional context in comments.

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.
Note that the -Paging parameter of Out-Host is not supported in the PowerShell ISE.
Also, comment on the answer: the alias oh is for Out-Host, and parameters can be abbreviated if there's no conflict, so the command could be Some-Cmdlet | oh -p
@TheXenocide How is out-host -paging more useful/usable than the others?
out-host is not available for me on Win 10
112

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

dir -rec | more

more does exactly that, when you take a look at the definition of the function with gcm more|select Definition|fl
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.
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 huddledmasses.org/…, wouldn't you think the function should be executed on W7 also? Hmm...
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.
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)
48

dir -rec | more is bad advice.

It will cause powershell to evaluate the entire command prior to outputting it to the screen, something that is not needed for something like output paginating

In some extreme cases, it could cause the system to crash (e.g. dir 'C:\' | more)

On the other hand, using out-host -paging will output information to the screen as it is available.

When you use remote powershell and want to pipe a command on the remote windows server through a pager, piping through out-host -paging works as desired. Piping through more when running the remote command is of no use: the entire text is displayed at once.
The alias for out-host -paging is oh -pa.
45

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.

You can install it by starting an admin shell and running:

Find-Package pscx | Install-Package -Force

(the force is to upgrade older versions)

If you don't have the admin rights, you can install it for the current user :

Find-Package pscx | Install-Package -Force -scope currentuser -allowclobber

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

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

It works in ConEmu and Powershell windows, but unfortunately it doesn't work the way you'd expect under the v2.0 ISE.

The installation overrides several basic Powershell commands (gcb,Expand-Archive,Format-Hex,Get-Hash,help,prompt,Get-Clipboard,Get-Help,Set-Clipboard) so saying "the force is to upgrade older versions" seems a little bit misleading.
just installing for current user without errors or warnings use: Find-Package pscx | Install-Package -Force -scope currentuser -allowclobber
Thanks for the awesome tip!
-allowclobber seems to be required in current powershell versions
30

I prefer the "less" command over the "more" command. With the less command, results can also be paged backwards instead of just forwards.

The "less" from Git for Windows works for me*

To save typing I added the alias "l" for less in my Powershell profile (notepad $profile):

Set-Alias -Name "l" -Value "${env:ProgramFiles(x86)}\Git\bin\less.exe"

Look for less either in the above path or C:\Program Files\Git\usr\bin\less.exe or similar.


*: I had errors in Powershell with the Gow version of "less".

Me too. Besides paging backwards, less allows me to perform text search on the result (forward slash, then type text, then press enter, then 'n' for next result, 'p' for previous result). Very, very, convenient.
Another less fan here. But I get this error. Any advice? ________________________________________ PS C:\Users\oli> Get-Content tmpf | out-host 'C:\Program Files\Git\usr\bin\less.exe' Out-Host : A positional parameter cannot be found that accepts argument 'C:\Program Files\Git\usr\bin\less.exe'. At line:1 char:20 + Get-Content tmpf | out-host 'C:\Program Files\Git\usr\bin\less.exe' + CategoryInfo : InvalidArgument: (:) [Out-Host], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutHostCommand
@olisteadman, This is the official Git version of less.exe. Have you tried the Git for Windows gitforwindows.org version of less?
No need to use "out-host". I use this: "Get-Content tmpf | C:\cygwin64\bin\less.exe"
It is also a very good idea to set $env:PAGER="C:\cygwin64\bin\less.exe", so it will be used for example when you type "help"
6
PS> cd C:\

PS> dir -r -ex 0 | out-Host -paging

PS> dir -file -r -ea 0 c:\Windows | Select FullName,Length,LastWriteTime | out-gridview

Comments

6

Another option is to use less through the WSL:

some-cmdlet | wsl less

Note that this only works if your default WSL distribution is WSL1. WSL2 distributions run into the problem mentioned in these two Github issues. It's also possible to specify a WSL1 distribution manually in the command-line with -d <distro>.
5

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.

Piping is extremely slow for large files. There are better ways (built-in options) to do head and tail in powershell now: stackoverflow.com/a/41626586/1081043
2

If you have VIM installed, I thoroughly enjoy dir -r | vim -R -. Unfortunately this suffers the same problem with more (ie. no streaming).

Comments

2
cat C:\Temp\test.txt

cat is an alias for Get-Content - with larger files you will get the -- More -- output at the bottom of the terminal

You can also you can add -wait

cat C:\Temp\test.txt -wait 

-wait is like using tail but it actually is rerunning the command just refreshing the output

cat C:\Temp\test.txt | oh –Paging

oh = Out-Host

with larger files i do not get the -- More -- at the bottom. Piping to oh -P worked for me.
2

The easiest/most elegant in-built way of paging output without relying on non-standard utilities is to pipe the output through the Out-Host utility. Usually, all output goes through this utility by default but, if manually specified, you can add the -Paging option that provides similar functionality to less or more under Unix-based OS:

Get-Content a_very_long_file.txt | Out-Host -Paging

PowerShell has many predefined aliases. In this case, this can be shortened to:

gc a_very_long_file.txt | oh -P

which means exactly the same (gc == Get-Content; oh == Output-Host)

To read more about the Out-Host utility:

Get-Help Out-Host

Comments

1

I added a function definition and alias to my default profile at %SystemRoot%\system32\windowspowershell\v1.0\profile.ps1

This function is mostly based on this blog entry by Aman Dhally with added exception handling for pressing Q while paging.

function more2
{
   param(
     [Parameter(ValueFromPipeline=$true)]
     [System.Management.Automation.PSObject]$InputObject
   )

   begin
   {
      $type = [System.Management.Automation.CommandTypes]::Cmdlet
      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand(‘Out-Host’, $type)
      $scriptCmd = {& $wrappedCmd @PSBoundParameters -Paging }
      $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
      $steppablePipeline.Begin($PSCmdlet)
   }

   process
   {
      try
      {
         $steppablePipeline.Process($_)
      }
      catch
      {
        break;
      }
   }

   end
   {
      $steppablePipeline.End()
   }

   #.ForwardHelpTargetName Out-Host
   #.ForwardHelpCategory Cmdlet
}

New-Alias more more2

so I can just call it like dir -r | more and it immediately starts paged output because of PowerShell's pipeline (as opposed to waiting for the complete output with more.com).

Interesting that people really invest their time in 2023 to downvote an answer that was valid in 2012. Sheesh.
1

I had exactly this question (well I wanted less, not more) and found the answer of @richard-berg worked for me, being new to PowerShell (but not to Linux), I found the things missing from that answer (for me) were: I first needed to go:

Find-Package pscx | Install-Package

which then prompted for "installing nuget package". I did this but then had to use the
-AllowClobber parameter on Install-Package.

then in order to use less, I had to:

Set-ExecutionPolicy RemoteSigned

which all worked :-)

Welcome to the site. Not sure what you mean by "answer #24". Those numbers are votes (which can change) rather than enumeration.
Ah, I see ... first time I've posted to stack overflow! I meant the answer of @richard-berg, for me, that would have been the accepted answer
Not sure why I get a down vote? Seems (to me) it is helpful to confirm when something has worked!
1

The easiest thing to do in my opinion is to use Scoop to install anything you're used to using from UNIX. Once you do, just run scoop install less and you're good to go.

Comments

0

Suggestion: Put the file into a temporary/disposable .txt file, then let the OS invoke your favorite editor, the one that is linked to the .txt extension.

Get-Process | Out-File temp.txt ; .\temp.txt

Note: each time you use this you will overwrite any pre-existent temp.txt file. Pick the file name wisely.

The above is just a basic idea.
Next step would be transforming this into | more using aliases or profile functions, etc.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.