powershellpowershell-isewindows-console

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


The maximum number of lines in my environment is 47.

Can I measure this value programmatically?


Solution

  • To complement Christian.K's helpful answer:


    If running in the PowerShell console host:

    You can access the console's properties either:

    Sample $Host.UI.RawUI output:

    PS> $Host.UI.RawUI
    
    ForegroundColor       : DarkYellow
    BackgroundColor       : DarkMagenta
    CursorPosition        : 0,58
    WindowPosition        : 0,0
    CursorSize            : 25
    BufferSize            : 160,9999
    WindowSize            : 160,75
    MaxWindowSize         : 160,94
    MaxPhysicalWindowSize : 303,94
    KeyAvailable          : True
    WindowTitle           : 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:

    PS> $Host.UI.RawUI  # in the ISE
    
    ForegroundColor       : -1
    BackgroundColor       : -1
    CursorPosition        : 0,0
    WindowPosition        : 
    CursorSize            : 
    BufferSize            : 166,0
    WindowSize            : 
    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).

    There is no point in trying to use the .NET [Console] class in the ISE, except to query / set the character encoding used for communication with external programs, [Console]::OutputEncoding:[1]


    [1] Note that the ISE defaults to the system's legacy ANSI code page, whereas regular console windows default to the OEM code page. Thus, these two environments decode output from external programs differently by default.