Equivalent of 'more' or 'less' command in Powershell?

后端 未结 12 1840
小蘑菇
小蘑菇 2020-12-13 01:38

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

相关标签:
12条回答
  • 2020-12-13 01:59

    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)

    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.

    0 讨论(0)
  • 2020-12-13 02:03

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

    dir -rec | more
    
    0 讨论(0)
  • 2020-12-13 02:03

    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):

    sal l "C:\Program Files (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".

    0 讨论(0)
  • 2020-12-13 02:05

    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).

    0 讨论(0)
  • 2020-12-13 02:07

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

    0 讨论(0)
  • 2020-12-13 02:10
    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
    
    0 讨论(0)
提交回复
热议问题