none
Determine Terminal Font Size RRS feed

  • Question

  • Hi Scripting Guys and Gals!

    I've got a Powershell script that I drop into my Powershell Profile so I can dynamically scale the Powershell window size to match that of my display. It's one thing about Powershell (and previously cmd.exe) that drives me a bit nuts, not being able to easily/automatically adjust the window size to match my display size.

    The thing is I can't find a way to determine the size of the default Powershell window font. I need to know the width to do this accurately every time. As it stands I hardcode it as being 8 pixels wide as this seems to be the most common screen font wdith (by default). Although 7 pixels is also pretty common. It would be much better if I could ascertain this programmatically! Help!

    # Get resolution of primary screen and set Powershell window size correspondingly
    
    # Load the System Windows Forms assembly
    Add-Type -AssemblyName System.Windows.Forms
    $DisplayDimensions = [System.Windows.Forms.Screen]::PrimaryScreen
    $RawDisplayWidth = $DisplayDimensions.WorkingArea.Width
    $DisplayHeight = $DisplayDimensions.WorkingArea.Height
    
    # Get width of the Windows window frame the Powershell window will run in
    $WindowFrameWidth = [System.Windows.Forms.SystemInformation]::FrameBorderSize.Width
    # Get width of the Windows horizontal scrollbar for the Powershell Window
    $HorizontalScrollBarWidth = [System.Windows.Forms.SystemInformation]::HorizontalScrollBarThumbWidth
    
    # Now set the display width based on the above metrics. Make sure you always round DOWN ([decimal]::floor method)
    $DisplayWidth = [decimal]::floor(($RawDisplayWidth - ($WindowFrameWidth + $HorizontalScrollBarWidth))/8)
    
    # I couldn't find a way to ascertain the screen font size (which determines console character width)
    # So I am making the unreliable (but generally correct) assumption that it is the default 8x12.
    # I could spend more time but cost/benefit ratio startio to get out of kilter. If you find it, add it please!
    
    $CustomPowershell = (Get-Host).UI.RawUI
    $CustomPowershell.WindowTitle = "My Resized Window"
    
    $NewWindowWidth = $DisplayWidth
    $NewWindowHeight = ($DisplayHeight/12)-5
    
    $BufferSize = $CustomPowershell.BufferSize
    $BufferSize.Width = $NewWindowWidth
    $BufferSize.Height = 1000
    $CustomPowershell.BufferSize = $BufferSize
    
    $WindowSize = $CustomPowershell.WindowSize
    $WindowSize.Width = $NewWindowWidth
    $WindowSize.Height = $NewWindowHeight
    $CustomPowershell.WindowSize = $WindowSize
    $CustomPowershell.WindowPosition.X = 0
    $CustomPowershell.WindowPosition.Y = 0

    Thanks and regards,

    Phil

    • Moved by Bill_Stewart Thursday, October 9, 2014 2:19 AM Abandoned
    Wednesday, July 9, 2014 2:01 AM

All replies

  • Font and windows size are set by the OS and not by PowerShell.


    ¯\_(ツ)_/¯

    Wednesday, July 9, 2014 2:10 AM
  • Here is where the fonts are registered but only if the user has changed from the default.

    HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe

    The default is set in the Windows theme or base settings.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, July 9, 2014 2:20 AM
    Wednesday, July 9, 2014 2:19 AM
  • Thanks jrv but you are missing the point of my question somewhat.

    I can programmatically determine the following dimensions of the screen and the window which Powershell will open in, I just can't programmatically determine the font being used and its width.

    Add-Type -AssemblyName System.Windows.Forms
    $DisplayDimensions = [System.Windows.Forms.Screen]::PrimaryScreen
    $RawDisplayWidth = $DisplayDimensions.WorkingArea.Width
    $DisplayHeight = $DisplayDimensions.WorkingArea.Height
    # Get width of the Windows window frame the Powershell window will run in
    $WindowFrameWidth = [System.Windows.Forms.SystemInformation]::FrameBorderSize.Width
    # Get width of the Windows horizontal scrollbar for the Powershell Window
    $HorizontalScrollBarWidth = [System.Windows.Forms.SystemInformation]::HorizontalScrollBarThumbWidth

    Wednesday, July 9, 2014 3:16 AM
  • No - I am responding exactly.  The font size is stored in the registry under the key I posted.  Iy is not available in any PowerShell variables.  Just read the registry key and you have the font size.

    PS C:\scripts> get-itemproperty HKCU:\'Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'


    PopupColors      : 243
    FontFamily       : 54
    QuickEdit        : 1
    ColorTable05     : 5645313
    ScreenBufferSize : 196608120
    WindowSize       : 3276920
    ColorTable06     : 15789550
    FontWeight       : 400
    ScreenColors     : 86
    FaceName         : Lucida Console
    FontSize         : 786432
    PSPath           : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShel
                       l_v1.0_powershell.exe
    PSParentPath     : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Console
    PSChildName      : %SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
    PSDrive          : HKCU
    PSProvider       : Microsoft.PowerShell.Core\Registry

    Just retrieve it and use it. Why is that not doing it programmatically?


    ¯\_(ツ)_/¯

    Wednesday, July 9, 2014 3:27 AM
  • You can use the API with p/Invoke code but it is pretty ugly:

    http://gallery.technet.microsoft.com/scriptcenter/cb72e4e6-4a68-4a2e-89b7-cc43a860349e#content


    ¯\_(ツ)_/¯

    Wednesday, July 9, 2014 3:37 AM