0

I find that often the bottom of a new console is pushed off the bottom of the screen so I put the below into my $Profile to fix that. I have tried to start using the new Windows Terminal for PowerShell work and have discovered that none of the console resizing tools that I use like this function. While this makes sense (as it is not a PowerShell console!), people are obviously going to want workarounds for this when using Windows Terminal (and I find the same problem that new Windows Terminals are often partly of the bottom of the screen).

Has anyone found workarounds for $host.UI.RawUI. that we can use to resize/move the new Windows Terminal app?

function Global:Set-ConsoleTopLeft {
    # Note: the DLL code below should NOT be indented from the left-side(!)
    Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")] 
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int W, int H); '
    $consoleHWND = [Console.Window]::GetConsoleWindow();
    $consoleHWND = [Console.Window]::MoveWindow($consoleHWND,75,0,600,600);
    # $consoleHWND = [Console.Window]::MoveWindow($consoleHWND,-6,0,600,600);
}

function Global:Set-MaxWindowSize {
    if ($Host.Name -match "console") {
        $MaxHeight = $host.UI.RawUI.MaxPhysicalWindowSize.Height - 1
        $MaxWidth = $host.UI.RawUI.MaxPhysicalWindowSize.Width - 20

        $MyBuffer = $Host.UI.RawUI.BufferSize
        $MyWindow = $Host.UI.RawUI.WindowSize

        $MyWindow.Height = ($MaxHeight)
        $MyWindow.Width = ($Maxwidth-2)

        $MyBuffer.Height = (9999)
        $MyBuffer.Width = ($Maxwidth-2)

        $host.UI.RawUI.set_bufferSize($MyBuffer)
        $host.UI.RawUI.set_windowSize($MyWindow)
    }
}

Set-ConsoleTopLeft
Set-MaxWindowSize
YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • In my script I use $host.UI.RawUI.BufferSize = $MyBuffer and It Works For Me (TM) as of this writing. – No Refunds No Returns Jan 06 '20 at 15:18
  • Sounds good. Could you send me a short/complete piece of code that I could try please? Would appreciate seeing that working as not sure how to implement what you said there into something. – YorSubs Jan 06 '20 at 16:05
  • instead of `$host.UI.RawUI.set_bufferSize($MyBuffer)` use what I said above. Otherwise my startup code is functionally equivalent to what you have. Just make sure your sizes are valid and this should be good. – No Refunds No Returns Jan 07 '20 at 03:10
  • Thanks. Got around to trying this, but just can't get this to work (it's still resizing the PS 5.1 window ok, but new Windows Terminal completely ignores this / does nothing, when I have a PS 5.1 session inside Windows Terminal). Could you send me your working / complete function if that's ok, just so that I can see what might be different in my setup please? – YorSubs Jan 10 '20 at 22:30
  • I'm not using Windows Terminal. Sorry I missed that detail. – No Refunds No Returns Jan 12 '20 at 01:21

0 Answers0