Page 1 of 1

Difficulty getting font dimensions with powershell

Posted: 01 Jun 2024 03:37
by shodan
Hi,

I've made these simple batch functions, that obtain font size, font width and font height using a powershell/C# hybrid oneliner

Using this command, you can set the font size to every size from 1 to 40
And then print out the font dimensions

Code: Select all

for /l %a in (1,1,40) do (SetFontSize %a & GetFontdimensions.bat )
However I am getting small discrepancies in the values and I cannot find the bug ?

Here is the result and what it should have been

Code: Select all

FontSize: 1 FontWidth: 1 FontHeight: 1
FontSize: 2 FontWidth: 1 FontHeight: 2
FontSize: 3 FontWidth: 2 FontHeight: 3
FontSize: 4 FontWidth: 2 FontHeight: 4
FontSize: 5 FontWidth: 2 FontHeight: 5
FontSize: 6 FontWidth: 3 FontHeight: 6
FontSize: 7 FontWidth: 3 FontHeight: 7 (FontHeight should have been 6)
FontSize: 8 FontWidth: 4 FontHeight: 8
FontSize: 9 FontWidth: 4 FontHeight: 9
FontSize: 10 FontWidth: 5 FontHeight: 10
FontSize: 11 FontWidth: 6 FontHeight: 11 (FontWidth should have been 5)
FontSize: 12 FontWidth: 6 FontHeight: 12
FontSize: 13 FontWidth: 6 FontHeight: 13
FontSize: 14 FontWidth: 6 FontHeight: 14 (FontWidth should have been 6)
FontSize: 15 FontWidth: 7 FontHeight: 15
FontSize: 16 FontWidth: 7 FontHeight: 16 (FontWidth should have been 7)
FontSize: 17 FontWidth: 8 FontHeight: 17
FontSize: 18 FontWidth: 9 FontHeight: 18 (FontWidth should have been 8)
FontSize: 19 FontWidth: 9 FontHeight: 19
FontSize: 20 FontWidth: 10 FontHeight: 20 (FontWidth should have been 9)
FontSize: 21 FontWidth: 10 FontHeight: 21 (FontHeight should have been 22)
FontSize: 22 FontWidth: 10 FontHeight: 22
FontSize: 23 FontWidth: 11 FontHeight: 23
FontSize: 24 FontWidth: 11 FontHeight: 24
FontSize: 25 FontWidth: 12 FontHeight: 25
FontSize: 26 FontWidth: 12 FontHeight: 26
FontSize: 27 FontWidth: 13 FontHeight: 27
FontSize: 28 FontWidth: 13 FontHeight: 28
FontSize: 29 FontWidth: 14 FontHeight: 29
FontSize: 30 FontWidth: 14 FontHeight: 30
FontSize: 31 FontWidth: 14 FontHeight: 31 (FontWidth should have been 15)
FontSize: 32 FontWidth: 15 FontHeight: 32
FontSize: 33 FontWidth: 15 FontHeight: 33
FontSize: 34 FontWidth: 16 FontHeight: 34
FontSize: 35 FontWidth: 17 FontHeight: 35 (FontWidth should have been 16)
FontSize: 36 FontWidth: 17 FontHeight: 36
FontSize: 37 FontWidth: 18 FontHeight: 37 (FontWidth should have been 17)
FontSize: 38 FontWidth: 18 FontHeight: 38
FontSize: 39 FontWidth: 18 FontHeight: 39
FontSize: 40 FontWidth: 18 FontHeight: 40 (FontWidth should have been 19)

Code: Select all

:SetFontSize
powershell -command "param([int]$size) Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class ConsoleFontSizeSetter { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct COORD { public short X; public short Y; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct CONSOLE_FONT_INFOEX { public uint cbSize; public uint nFont; public COORD dwFontSize; public uint FontFamily; public uint FontWeight; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string FaceName; } [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); [DllImport(\"kernel32.dll\", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); private const int STD_OUTPUT_HANDLE = -11; public static void SetFontSize(short size) { IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsoleOutput == IntPtr.Zero) { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get console handle. Error code: \" + errorCode); System.Environment.Exit(1); } CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) { cfi.dwFontSize.X = size; cfi.dwFontSize.Y = size; if (SetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) { System.Console.WriteLine(\"Font size set successfully.\"); System.Environment.Exit(0); } else { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to set font size. Error code: \" + errorCode); System.Environment.Exit(1); } } else { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get current font info. Error code: \" + errorCode); System.Environment.Exit(1); } } }'; [ConsoleFontSizeSetter]::SetFontSize(%1)"
GoTo :EOF

Code: Select all

:GetFontSize
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class ConsoleFontSize { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct COORD { public short X; public short Y; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct CONSOLE_FONT_INFOEX { public uint cbSize; public uint nFont; public COORD dwFontSize; public uint FontFamily; public uint FontWeight; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string FaceName; } [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); [DllImport(\"kernel32.dll\", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); private const int STD_OUTPUT_HANDLE = -11; public static void GetFontSize() { IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsoleOutput == IntPtr.Zero) { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get console handle. Error code: \" + errorCode); System.Environment.Exit(1); } CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) { System.Console.WriteLine(\"Font Size: \" + cfi.dwFontSize.Y); System.Environment.Exit(0); } else { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get font size. Error code: \" + errorCode); System.Environment.Exit(1); } } }'; [ConsoleFontSize]::GetFontSize()"
GoTo :EOF

Code: Select all

:GetFontWidth
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class ConsoleFontWidth { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct COORD { public short X; public short Y; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct CONSOLE_FONT_INFOEX { public uint cbSize; public uint nFont; public COORD dwFontSize; public uint FontFamily; public uint FontWeight; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string FaceName; } [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); [DllImport(\"kernel32.dll\", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); private const int STD_OUTPUT_HANDLE = -11; public static void GetFontWidth() { IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsoleOutput == IntPtr.Zero) { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get console handle. Error code: \" + errorCode); System.Environment.Exit(1); } CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) { System.Console.WriteLine(\"Font Width: \" + cfi.dwFontSize.X); System.Environment.Exit(0); } else { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get font width. Error code: \" + errorCode); System.Environment.Exit(1); } } }'; [ConsoleFontWidth]::GetFontWidth()"
GoTo :EOF

Code: Select all

:GetFontHeight
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class ConsoleFontHeight { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct COORD { public short X; public short Y; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct CONSOLE_FONT_INFOEX { public uint cbSize; public uint nFont; public COORD dwFontSize; public uint FontFamily; public uint FontWeight; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string FaceName; } [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); [DllImport(\"kernel32.dll\", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); private const int STD_OUTPUT_HANDLE = -11; public static void GetFontHeight() { IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsoleOutput == IntPtr.Zero) { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get console handle. Error code: \" + errorCode); System.Environment.Exit(1); } CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) { System.Console.WriteLine(cfi.dwFontSize.Y); System.Environment.Exit(0); } else { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get font height. Error code: \" + errorCode); System.Environment.Exit(1); } } }'; [ConsoleFontHeight]::GetFontHeight()"
GoTo :EOF

Code: Select all

:GetFontDimensions
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class ConsoleFontDimensions { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct COORD { public short X; public short Y; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct CONSOLE_FONT_INFOEX { public uint cbSize; public uint nFont; public COORD dwFontSize; public uint FontFamily; public uint FontWeight; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string FaceName; } [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); [DllImport(\"kernel32.dll\", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); private const int STD_OUTPUT_HANDLE = -11; public static void GetFontDimensions() { IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsoleOutput == IntPtr.Zero) { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get console handle. Error code: \" + errorCode); System.Environment.Exit(1); } CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) { System.Console.WriteLine(\"FontSize: \" + cfi.dwFontSize.Y + \" FontWidth: \" + cfi.dwFontSize.X + \" FontHeight: \" + cfi.dwFontSize.Y); System.Console.WriteLine(\"FontFamily: \" + cfi.FontFamily + \" FontWeight: \" + cfi.FontWeight + \" FaceName: \" + cfi.FaceName); System.Environment.Exit(0); } else { int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); System.Console.WriteLine(\"Failed to get font dimensions. Error code: \" + errorCode); System.Environment.Exit(1); } } }'; [ConsoleFontDimensions]::GetFontDimensions()"
GoTo :EOF



Here is the powershell version

GetFontDimensions

Code: Select all

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class ConsoleFontDimensions {
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct COORD {
        public short X;
        public short Y;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct CONSOLE_FONT_INFOEX {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public uint FontFamily;
        public uint FontWeight;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string FaceName;
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    private const int STD_OUTPUT_HANDLE = -11;

    public static void GetFontDimensions() {
        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hConsoleOutput == IntPtr.Zero) {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get console handle. Error code: " + errorCode);
            Environment.Exit(1);
        }

        CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX();
        cfi.cbSize = (uint)Marshal.SizeOf<CONSOLE_FONT_INFOEX>();

        if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) {
            Console.WriteLine("FontSize: " + cfi.dwFontSize.Y + " FontWidth: " + cfi.dwFontSize.X + " FontHeight: " + cfi.dwFontSize.Y);
            Console.WriteLine("FontFamily: " + cfi.FontFamily + " FontWeight: " + cfi.FontWeight + " FaceName: " + cfi.FaceName);
            Environment.Exit(0);
        } else {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get font dimensions. Error code: " + errorCode);
            Environment.Exit(1);
        }
    }
}
"@

[ConsoleFontDimensions]::GetFontDimensions()
SetFontSize

Code: Select all

param(
    [int]$size
)

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class ConsoleFontSizeSetter {
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct COORD {
        public short X;
        public short Y;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct CONSOLE_FONT_INFOEX {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public uint FontFamily;
        public uint FontWeight;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string FaceName;
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    private const int STD_OUTPUT_HANDLE = -11;

    public static void SetFontSize(short size) {
        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hConsoleOutput == IntPtr.Zero) {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get console handle. Error code: " + errorCode);
            Environment.Exit(1);
        }

        CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX();
        cfi.cbSize = (uint)Marshal.SizeOf<CONSOLE_FONT_INFOEX>();

        // First, retrieve the current console font information
        if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) {
            // Update the font size only
            cfi.dwFontSize.X = size;
            cfi.dwFontSize.Y = size;

            // Set the updated font information
            if (SetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) {
                Console.WriteLine("Font size set successfully.");
                Environment.Exit(0);
            } else {
                int errorCode = Marshal.GetLastWin32Error();
                Console.WriteLine("Failed to set font size. Error code: " + errorCode);
                Environment.Exit(1);
            }
        } else {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get current font info. Error code: " + errorCode);
            Environment.Exit(1);
        }
    }
}
"@

[ConsoleFontSizeSetter]::SetFontSize($size)
GetFontSize

Code: Select all

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class ConsoleFontSize {
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct COORD {
        public short X;
        public short Y;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct CONSOLE_FONT_INFOEX {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public uint FontFamily;
        public uint FontWeight;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string FaceName;
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    private const int STD_OUTPUT_HANDLE = -11;

    public static void GetFontSize() {
        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hConsoleOutput == IntPtr.Zero) {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get console handle. Error code: " + errorCode);
            Environment.Exit(1);
        }

        CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX();
        cfi.cbSize = (uint)Marshal.SizeOf<CONSOLE_FONT_INFOEX>();

        if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) {
            Console.WriteLine("Font Size: " + cfi.dwFontSize.Y);
            Environment.Exit(0);
        } else {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get font size. Error code: " + errorCode);
            Environment.Exit(1);
        }
    }
}
"@

[ConsoleFontSize]::GetFontSize()
GetFontWidth

Code: Select all

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class ConsoleFontWidth {
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct COORD {
        public short X;
        public short Y;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct CONSOLE_FONT_INFOEX {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public uint FontFamily;
        public uint FontWeight;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string FaceName;
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    private const int STD_OUTPUT_HANDLE = -11;

    public static void GetFontWidth() {
        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hConsoleOutput == IntPtr.Zero) {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get console handle. Error code: " + errorCode);
            Environment.Exit(1);
        }

        CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX();
        cfi.cbSize = (uint)Marshal.SizeOf<CONSOLE_FONT_INFOEX>();

        if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) {
            Console.WriteLine("Font Size: " + cfi.dwFontSize.X + "x" + cfi.dwFontSize.Y);
            Console.WriteLine("Font Width: " + cfi.dwFontSize.X);
            Console.WriteLine("Font Height: " + cfi.dwFontSize.Y);
            Console.WriteLine("Font Name: " + cfi.FaceName);
            Environment.Exit(0);
        } else {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get font width. Error code: " + errorCode);
            Environment.Exit(1);
        }
    }
}
"@

[ConsoleFontWidth]::GetFontWidth()
GetFontHeight

Code: Select all

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class ConsoleFontHeight {
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct COORD {
        public short X;
        public short Y;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct CONSOLE_FONT_INFOEX {
        public uint cbSize;
        public uint nFont;
        public COORD dwFontSize;
        public uint FontFamily;
        public uint FontWeight;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string FaceName;
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    private const int STD_OUTPUT_HANDLE = -11;

    public static void GetFontHeight() {
        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hConsoleOutput == IntPtr.Zero) {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get console handle. Error code: " + errorCode);
            Environment.Exit(1);
        }

        CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX();
        cfi.cbSize = (uint)Marshal.SizeOf<CONSOLE_FONT_INFOEX>();

        if (GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi)) {
            Console.WriteLine(cfi.dwFontSize.Y);
            Environment.Exit(0);
        } else {
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine("Failed to get font height. Error code: " + errorCode);
            Environment.Exit(1);
        }
    }
}
"@

[ConsoleFontHeight]::GetFontHeight()