Trying to move the current, active powershell window to the left side of the screen using a PowerShell script.
I've found this function, but it doesn't really come with any examples.
Trying to move the current, active powershell window to the left side of the screen using a PowerShell script.
I've found this function, but it doesn't really come with any examples.
Funny and interesting question.
If you want move window, you need to know the Window handle hWnd
of it. For console you can do it by using the GetConsoleWindow
function from kernel32.dll.
This script, it will move powershell console to upper-left with size (500, 500)
.
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,0,0,500,500);
If you know the hWnd of the window you can do many things with that window. You can find all functions here.
But this script work will only work with a real powershell console. If you will start it from Powershell ISE
, hWnd will be Zero 0
, because Powershell ISE don't have real console in it.
To give you a complete example with Set-Window
which mimics the manual Window+Left-Arrow.
On my 1920*1200 screen the window dimensions after Window+Left-Arrow are:
# Left Top Width Heigth
# -7 0 974 1207
So you need to get the screen dimensions first, for the primary monitor this will do:
Add-Type -AssemblyName System.Windows.Forms
$ScreenSize = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
$MyLeft = -7
$MyTop = 0
$MyWidth = $ScreenSize.Width/2+2*7
$MyHeight= $ScreenSize.Height +7
"Left:{0} Top:{1} Width:{2} Height:{3}" -f $MyLeft,$MyTop,$MyWidth,$MyHeight
Get-Process powershell | Set-Window -X $MyLeft -Y $MyTop -Width $MyWidth -Height $MyHeight
Yet another way for moving window of the current posh process:
$MethodDefinition = @'
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int wFlags);
'@
$User32 = Add-Type -MemberDefinition $MethodDefinition -Name 'User32' -Namespace 'Win32' -PassThru
$CurrentProcess = Get-Process -id $PID
$User32::SetWindowPos($CurrentProcess.MainWindowHandle, 0x1, 0, 0, 0, 0, 0x0040 -bor 0x0020 -bor 0x0001)