ウィンドウを並べる

# User32.dll を読み込む
Add-Type @"
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
"@

# GetWindowText 関数の宣言
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

# SetWindowPos 関数の宣言
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

# ターゲットのプロセスを取得
$desktopProcess = Get-Process -name "explorer"
$documentsProcess = Get-Process -name "explorer"

# ターゲットのウィンドウハンドルを取得
$desktopHandle = $desktopProcess.MainWindowHandle
$documentsHandle = $documentsProcess.MainWindowHandle

# 画面のサイズを取得
$screenWidth = [System.Windows.Forms.SystemInformation]::VirtualScreen.Width
$screenHeight = [System.Windows.Forms.SystemInformation]::VirtualScreen.Height

# 上半分のウィンドウサイズと位置を計算
$upperHeight = $screenHeight / 2
$upperRect = New-Object System.Drawing.Rectangle(0, 0, $screenWidth, $upperHeight)

# 下半分のウィンドウサイズと位置を計算
$lowerHeight = $screenHeight - $upperHeight
$lowerRect = New-Object System.Drawing.Rectangle(0, $upperHeight, $screenWidth, $lowerHeight)

# 上半分のウィンドウを移動
[User32]::SetWindowPos($desktopHandle, [IntPtr]::Zero, $upperRect.X, $upperRect.Y, $upperRect.Width, $upperRect.Height, 0x0040)

# 下半分のウィンドウを移動
[User32]::SetWindowPos($documentsHandle, [IntPtr]::Zero, $lowerRect.X, $lowerRect.Y, $lowerRect.Width, $lowerRect.Height, 0x0040)