The maximum number of lines in my environment is 47.
Can I measure this value programmatically?
To complement Christian.K’s helpful answer:
- A simpler and slightly more efficient method for obtaining the host object is to use the
$Hostautomatic variable rather than the
- 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
$Trueindicates that the host is a console.
If running in the PowerShell console host:
You can access the console’s properties either:
$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:
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
BufferSize : 166,0
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,
- Initially in a session, trying to use the window-related members of
[Console]causes exceptions, because the ISE does not allocate a console window on startup:
Default1234# In a pristine session.PS> [Console]::WindowHeightThe handle is invalid. # EXCEPTION
- While the ISE allocates a – hidden – console window on demand, namely the first time you run a console application in your session, that hidden console window’s properties, as then reported on via
[Console], do not reflect the properties of the simulated console that the ISE presents to the user.
Default1234567# chcp is a console application, so when it is run,# the ISE allocates a (hidden) console window,# after which the [Console] API becomes technically usable,# but DOESN'T REPORT MEANINGFUL VALUES.PS> chcp >$null; [Console]::WindowHeight72 # No exception, but the value is meaningless.
 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.