How can I measure the window height (number of lines) in powershell? How can I measure the window height (number of lines) in powershell? powershell powershell

How can I measure the window height (number of lines) in powershell?


To complement Christian.K's helpful answer:

  • A simpler and slightly more efficient method for obtaining the host object is to use the $Host automatic variable rather than the Get-Host cmdlet.

  • You're only guaranteed to have access to window-size information in the PowerShell console host, i.e., when running in a console (terminal) window

    • It is at a given host's discretion whether to expose this information, and, as Christian states, the PowerShell ISE does not - even though it arguably should, given that it has a console subsystem built in.
  • To test whether your code is running in a console (a.k.a terminal) or not, use
    $Host.UI.SupportsVirtualTerminal - $True indicates that the host is a console.


If running in the PowerShell console host:

You can access the console's properties either:

  • via $Host.UI.RawUI (as in Christian's answer), which is an object of type [System.Management.Automation.Internal.Host.InternalHostRawUserInterface] that - in the PowerShell console host only - wraps the .NET [Console] class (see below).

  • via the .NET [Console] class; e.g., to get the window height (count of rows) this way, use:

    • [Console]::WindowHeight

Sample $Host.UI.RawUI output:

> $Host.UI.RawUIForegroundColor       : DarkYellowBackgroundColor       : DarkMagentaCursorPosition        : 0,58WindowPosition        : 0,0CursorSize            : 25BufferSize            : 160,9999WindowSize            : 160,75MaxWindowSize         : 160,94MaxPhysicalWindowSize : 303,94KeyAvailable          : TrueWindowTitle           : Windows PowerShell

If running in the PowerShell ISE:

Written as of PowerShell version 5.1

Most of the properties in $Host.UI.RawUI are not populated and will return their data type's default value:

> $Host.UI.RawUI  # in ISEForegroundColor       : -1BackgroundColor       : -1CursorPosition        : 0,0WindowPosition        : CursorSize            : BufferSize            : 166,0WindowSize            : MaxWindowSize         : MaxPhysicalWindowSize : KeyAvailable          : WindowTitle           : Windows PowerShell ISE

The only size-related information that is available is the buffer width (166 in the sample output above).

Note that if you tried to use [Console] directly from the ISE, you'd get exceptions:

> [Console]::WindowHeightThe handle is invalid.     # EXCEPTION...


You could try something like

$(Get-Host).UI.RawUI.WindowSize.Height

Note a couple of things however:

  • Using the RawUI might not be particular "portable", depending on where your script runs. In "ISE" for example, that property returns nothing, while on the console it will work.

    For more difference here between the console and ISE, run $(Get-Host).UI.RawUI and compare the output.

    In other PowerShell hosts, besides the console or ISE, it might yet be still different.

  • There is also a BufferSize, which can be bigger than the WindowSize. The later being only the part that is currently "viewable" by the user.

Your question sounds a little like a xy problem. Consider explaining (as an edit to your question or a new question), what you're actually trying to achieve, i.e. why you need to know the number of lines?


The functionality I really need is git status, git diff in powershell_ise with highlight and output paging.It seems that the current version of powershell_ise does not solve the problem itself.

Although not completely satisfactory, I have devised a practical solution and created the following function.




source code

function hhd-git-diff{    [CmdletBinding()]    param    (    )    $res = git diff    hhd-git-colored-output -INPUT_OBJECT $res}function hhd-git-status{    [CmdletBinding()]    param    (    )    $res = git status    hhd-git-colored-output -INPUT_OBJECT $res}function hhd-git-colored-output{    [CmdletBinding()]    param    (        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)]        [System.Object]        $INPUT_OBJECT    )    $lineNum = 1    $INPUT_OBJECT |     Out-String |     foreach {        $_ -split "\n" |        foreach {            if($lineNum % [Console]::WindowHeight -eq 0)            {                Read-Host "continue? press any key..."            }            $lineNum++            if($_ -like "diff --git *")            {                Write-Host ""                Write-Host ""                Write-Host ""                Write-Host ("#"*80) -ForegroundColor Cyan                Write-Host $_ -ForegroundColor Cyan                Write-Host ("#"*80) -ForegroundColor Cyan                Read-Host "continue? press any key..."                $lineNum = 1            }            elseif($_ -like "--- *")            {            }            elseif($_ -like "+++ *")            {            }            elseif($_ -like "-*")            {                Write-Host $_ -ForegroundColor Red            }            elseif($_ -like "+*")            {                Write-Host $_ -ForegroundColor Green            }            elseif($_ -like "On branch *")            {                Write-Host ("#"*80) -ForegroundColor Cyan                Write-Host $_ -ForegroundColor Cyan            }            elseif($_ -like "Your branch is*")            {                Write-Host $_ -ForegroundColor Cyan                Write-Host ("#"*80) -ForegroundColor Cyan            }            elseif($_ -like "Changes to be committed:*")            {                Write-Host ("-"*80) -ForegroundColor Green                Write-Host $_ -ForegroundColor Green            }            elseif($_ -like "Changes not staged for commit:*")            {                Write-Host ("-"*80) -ForegroundColor Red                Write-Host $_ -ForegroundColor Red            }            elseif($_ -like "Untracked files:*")            {                Write-Host ("-"*80) -ForegroundColor Black                Write-Host $_ -ForegroundColor Black            }            elseif($_ -like "*modified:*")            {                Write-Host $_ -ForegroundColor Yellow            }            elseif($_ -like "*deleted:*")            {                Write-Host $_ -ForegroundColor Red            }            else            {                Write-Host $_ -ForegroundColor Gray            }        }    }}



screen shot