안녕하세요!


이번 글에서는 유용하게 쓰일 수 있는 PowerShell 스크립트를 소개해 드리려고 합니다!


어떤 분께서 4개의 PowerShell 창을 분할하여 보여줄 수 있지 않을까요? 라는 말씀을 하셔서, 저도 궁금해서 한 번 작성을 해봤습니다.


처음엔 WinKey + 방향키를 이용하여 분할하는 방법을 얘기해봤는데 이미 해보셨던 작업이고, 매 번 PowerShell을 4개를 열고 화면에 배치하는 작업을 하신다고 하시곤 스크립트로 자동화를 해보신다고 하시더라구요.


Move-Window.ps1
param($x, $y, $width, $height)

$signature = @'
[DllImport("user32")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport("kernel32")]
public static extern IntPtr GetConsoleWindow();
'@


$previousErrorActionPref = $ErrorActionPreference

$ErrorActionPreference = "SilentlyContinue"
$type = Add-Type -MemberDefinition $signature `
        -Name PInvoke -Namespace 'PInvokeWrapper.Kernel32' `
        -PassThru -ErrorAction 'SilentlyContinue'
$ErrorActionPreference = $previousErrorActionPref

if (!$type) {
    $type = [PInvokeWrapper.Kernel32.PInvoke]
}

$type::MoveWindow($type::GetConsoleWindow(), $x, $y, $width, $height, $true)

위의 스크립트는 콘솔 창의 위치와 크기를 지정된 값으로 설정해주는 스크립트입니다.


GetConsoleWindow API를 사용하여 현재 콘솔창의 창 핸들을 가져오고, MoveWindow API를 사용하여 위치 및 크기를 설정해주는 작업을 해주고 있죠.




다음 스크립트는 PowerShell 4개를 실행한 후 주 화면의 작업 영역을 가져오고 각 PowerShell을 분할해주는 작업을 수행하는 스크립트입니다.


Split-PowerShells.ps1
param()

# Add System.Windows.Forms assembly
Add-Type -AssemblyName System.Windows.Forms

# Get active bounds
$screenInfo = [System.Windows.Forms.Screen]::PrimaryScreen

$halfWidth = $screenInfo.WorkingArea.Width / 2
$halfHeight = $screenInfo.WorkingArea.Height / 2

Start-Process -FilePath "powershell" -ArgumentList "-NoExit .\Move-Window 0 0 $halfWidth $halfHeight"
Start-Process -FilePath "powershell" -ArgumentList "-NoExit .\Move-Window $halfWidth 0 $halfWidth $halfHeight"
Start-Process -FilePath "powershell" -ArgumentList "-NoExit .\Move-Window 0 $halfHeight $halfWidth $halfHeight"
Start-Process -FilePath "powershell" -ArgumentList "-NoExit .\Move-Window $halfWidth $halfHeight $halfWidth $halfHeight"


두 번째 스크립트는 정말 간단합니다.


주 화면의 작업 영역을 가져오고 반으로 나눈 후 각각 TL, TR, BL, BR 구간으로 이동시켜주는 작업을 수행합니다.


다만, 약간의 버그(?)가 있습니다. 작업 영역에 꽉 차는게 아니라 콘솔 창 사이와 모니터 경계 간 약간의 여백이 발생합니다.


< 스크린샷 >


Split-PowerShells.zip




잘 활용하셨으면 좋겠습니다.


감사합니다 :)

+ Recent posts