Search code examples
powershellconsole-applicationkernel32

Change the PowerShell console font programmatically


I've developed a script that acts as a console based application (think ncurses) using PowerShell.

The font Lucida Console is configured within a shortcut to the script.

I'd like to provide the user with the ability to change the font size. The following module does just that, however the font is always reset to 'Raster Fonts': https://4sysops.com/archives/change-powershell-console-font-size-with-cmdlet/

I don't understand why this happens, as the code appears to produce the list of legal font sizes based on a reference to the current font information.

@sodawillow's comment sent me on the right course. To elaborate...

It appears that list of valid font sizes is determined by the console window size (there may also be other factors that I have failed to consider).

For example, get-consolefontinfo | format-table returns the following on a window size of 120x64:

nFont dwFontSizeX dwFontSizeY
----- ----------- -----------
0          84           42
1          70           48
2          52           64
3          105          64
4          105          64
5          120          64
6          120          64
7          168          64
8          52           96
9          105          96
10         140          96
11         210          128

Whereas with a window size of 106x51, it returns:

nFont dwFontSizeX dwFontSizeY
----- ----------- -----------
0          104          49
1          114          49
2          125          49
3          104          55
4          78           73
5          156          73
6          179          73
7          250          73
8          78           110
9          156          110
10         209          110
11         313          147

The font (Consolas, Lucida Console or Raster Fonts) that's applied will differ depending on the index selected.

So contrary to my question, this module does not always reset the font to 'Raster Fonts'.


Solution

  • I did some testing. I think you cannot directly change the size with this module (my default console font is Consolas):

    Set-ConsoleFont 1 #Raster Fonts
    ...
    Set-ConsoleFont 9 #Raster Fonts
    Set-ConsoleFont 10 #Consolas
    Set-ConsoleFont 11 #Consolas
    

    the code appears to produce the list of legal font sizes based on a reference to the current font information.

    According to the module's description:

    Get-ConsoleFontInfo

    List current console's available fonts.