YorSubs
YorSubs

Reputation: 4050

Move / manipulate PowerShell console windows on opening

Of late, Windows has decided to open every PowerShell console such that the prompt is invisible and off the screen below the taskbar. This is quite annoying. I would like something that I can put into my $profile that will either position every new console centrally in the screen, or snap it to the left (or right) of the screen, or maximise it, or (would require a bit more logic I guess) "if part of console is invisible move it up until it is visible".

I know we can get the monitor size as follows, but can anyone show me a good way to use these (or some other method) to reposition any PowerShell console window that I open is not subject to this problem on opening?

$monitor = Get-Wmiobject Win32_Videocontroller
$monitor.CurrentHorizontalResolution    => 1920
$monitor.CurrentVerticalResolution      => 1080

I've also seen things like this. Can this be exploited to get the PowerShell console that was just opened and move it?

$IE = New-Object -ComObject Internetexplorer.application
$IE.Left = 0
$IE.Width = 500
$IE.Top = 0
$IE.Height = 500
$IE.Navigate($URL)
$IE.Visible = $True

Or something like this

(Get-Process).MainWindowTitle -like '*PowerShell*'

But this is not ideal as it relies on "PowerShell" in the title which might not be the case if I change the title (I like to put the uptime in the Title).

I'd also love a way to open 2 PowerShell consoles and then snap them left and right, or 4 PowerShell consoles and snap them to four corners (maybe someone has built tricks like this already that would be nice to have)?

Upvotes: 1

Views: 2501

Answers (2)

YorSubs
YorSubs

Reputation: 4050

Found some answers, so I'll just put them here in case of use to others. I've not found a way to position two console windows to each side, or four consoles to each corner yet, so if anyone has that, would appreciate.

First, I found both of these in another StackOverflow answer https://gallery.technet.microsoft.com/scriptcenter/Set-the-PowerShell-Console-bd8b2ad1 , and part of that is nice and concise in that it can move and resize a window. Note in particular the use of -6 for the X position. This is not in the answer, but I know that this corresponds to snapping a window to the edge of the screen so that it's tight to the edge so I now use that (someone on that thread suggests -7 but that's not correct as you can clearly see if you snap to edge after going to -7, that the window pulls out from the edge, so -6 is the correct position):

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, -6, 0, 600, 650);

That's great, and I could probably do everything with the above, but I have to also calculate screen width etc, but this function nicely does that to fit everything to the screen (it cannot move the window but it can alter the size nicely to match the size of the screen), so in the end I've settled on running the above first in my $profile, followed by the below function.

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

        $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)
      }
  }

Upvotes: 2

Wasif
Wasif

Reputation: 15470

Have found that AutoHotKey is very good for window positioning tasks.

Here is an example script. Call it psrun.ahk and then run it from the Powershell or double click on it.

Run, Powershell.exe
WinWait, ahk_class WindowTitle
WinActivate
WinMove A, X, Y, X, Y

It will start an powershell window and you can adjust window movement by changing X and Y params and WindowTitle

Upvotes: 1

Related Questions