Powershell: Maximize Window
In PowerShell, you can maximize the window of the console using the WinAPI function SetWindowPlacement.
Here is an example script that demonstrates how to achieve this:
$signature = @"
using System;
using System.Runtime.InteropServices;
public class WindowHelper {
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct WindowPlacement {
public int length;
public int flags;
public int showCmd;
public Point minPosition;
public Point maxPosition;
public Rectangle normalPosition;
}
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Point {
public int x;
public int y;
}
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Rectangle {
public int left;
public int top;
public int right;
public int bottom;
}
public const int SW_MAXIMIZE = 3;
public static void MaximizeWindow(IntPtr hWnd) {
WindowPlacement placement = new WindowPlacement();
GetWindowPlacement(hWnd, ref placement);
placement.flags = 0;
placement.showCmd = SW_MAXIMIZE;
SetWindowPlacement(hWnd, ref placement);
}
}
public class Program {
public static void Main() {
IntPtr handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
WindowHelper.MaximizeWindow(handle);
}
}
"@
Add-Type -TypeDefinition $signature -PassThru | Out-Null
[Program]::Main()
The example script starts by defining a C# class called WindowHelper
that contains methods and structures from the WinAPI. It includes functions to show the window, get the window placement, as well as the necessary structures for passing data. Inside the Main
method of the Program
class, it obtains the handle of the current PowerShell console window and calls the MaximizeWindow
function from WindowHelper
to maximize it.
To use this script, simply copy and paste it into a PowerShell session. Once executed, the window will be maximized.
Note: Remember to save any unsaved work in the console window before executing the script, as it will automatically maximize the window without prompt.