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

后端 未结 12 2075
小蘑菇
小蘑菇 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 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).

提交回复
热议问题