Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active December 14, 2017 19:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jdhitsolutions/9314e79e8c6718845485 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/9314e79e8c6718845485 to your computer and use it in GitHub Desktop.
A function to pipe objects to the pipeline and console in groups or pages.
#requires -version 4.0
Function Out-More {
<#
.Synopsis
Send "pages" of objects to the pipeline.
.Description
This function is designed to display groups or "pages" of objects to the PowerShell pipeline. It is modeled after the legacy More.com command line utility.
By default the command will write out objects out to the pipeline in groups of 50. You will be prompted after each grouping. Pressing M or Enter will get the next group. Pressing A will stop paging and display all of the remaining objects. Pressing N will display the next object. Press Q to stop writing anything else to the pipeline.
.Parameter ClearScreen
Clear the screen prior to writing data to the pipeline. This parameter has an alias of cls.
.Parameter Count
The number of objects to group together in a page. This parameter has an alias of i.
.Example
PS C:\> get-process | out-more -count 10
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id SI ProcessName
------- ------ ----- ----- ----- ------ -- -- -----------
1031 75 122588 81092 841 31.30 1872 1 BoxSync
57 3 488 968 12 0.02 1068 1 BoxSyncMonitor
103 9 1448 4220 67 0.02 1632 0 BtwRSupportService
80 9 3008 8588 ...27 21.00 5192 1 conhost
40 5 752 2780 ...82 0.00 5248 0 conhost
53 7 972 3808 ...07 0.02 6876 1 conhost
482 17 1932 3692 56 0.91 708 0 csrss
520 30 2488 134628 180 31.67 784 1 csrss
408 18 6496 12436 ...35 0.56 1684 0 dasHost
180 14 3348 6748 66 0.50 4688 0 devmonsrv
[M]ore [A]ll [N]ext [Q]uit
Display processes in groups of 10.
.Example
PS C:\> dir c:\work -file -Recurse | out-more -ClearScreen | tee -Variable work
List all files in C:\Work and page them to Out-More using the default count, but after clearing the screen first.
The results are then piped to Tee-Object which saves them to a variable.
.Notes
Last Updated: December 22, 2015
Version : 1.0
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.Link
http://jdhitsolutions.com/blog/powershell/4707/a-better-powershell-more/
.Inputs
System.Object[]
.Outputs
System.Object
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory,ValueFromPipeline)]
[object[]]$InputObject,
[ValidateRange(1,1000)]
[Alias("i")]
[int]$Count = 50,
[Alias("cls")]
[Switch]$ClearScreen
)
Begin {
if ($ClearScreen) {
Clear-Host
}
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
Write-Verbose "Using a count of $count"
#initialize an array to hold objects
$data = @()
#initialize some variables to control flow
$ShowAll = $False
$ShowNext = $False
$Ready = $False
$Quit = $False
} #begin
Process {
if ($Quit) {
Write-Verbose "Quitting"
Break
}
elseif ($ShowAll) {
$InputObject
}
elseif ($ShowNext) {
Write-Verbose "Show Next"
$ShowNext = $False
$Ready = $True
$data = ,$InputObject
}
elseif ($data.count -lt $count) {
Write-Verbose "Adding data"
$data+=$Inputobject
}
else {
#write the data to the pipeline
$data
#reset data
$data= ,$InputObject
$Ready = $True
}
If ($Ready) {
#pause
Do {
Write-Host "[M]ore [A]ll [N]ext [Q]uit " -ForegroundColor Green -NoNewline
$r = Read-Host
if ($r.Length -eq 0 -OR $r -match "^m") {
#don't really do anything
$Asked = $True
}
else {
Switch -Regex ($r) {
"^n" {
$ShowNext = $True
$InputObject
$Asked = $True
}
"^a" {
$InputObject
$Asked = $True
$ShowAll = $True
}
"^q" {
#bail out
$Asked = $True
$Quit = $True
}
Default {
$Asked = $False
}
} #Switch
} #else
} Until ($Asked)
$Ready = $False
$Asked = $False
} #else
} #process
End {
#display whatever is left in $data
if ($data -AND -Not $ShowAll) {
Write-Verbose "Displaying remaining data"
$data
}
Write-Verbose "Ending: $($MyInvocation.Mycommand)"
} #end
} #end Out-More
Set-Alias -Name om -Value Out-More
<#
Copyright (c) 2016 JDH Information Technology Solutions, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#>
@jdhitsolutions
Copy link
Author

@jdhitsolutions
Copy link
Author

I added the function to my profile script for all PowerShell hosts:
". c:\scripts\out-more.ps1" | out-file $profile.CurrentUserAllHosts -Append

@jdhitsolutions
Copy link
Author

This is now part of the PSScriptTools module https://github.com/jdhitsolutions/PSScriptTools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment