Share this page 

Launch an executable, resize and position from CMD file (Win/PS)Tag(s): Misc Prog HowTo

A CMD file to launch an executable, resize its window and position it on the screen.

This a special CMD file since Powershell code is embedded in it. This code will call the Windows API MoveWindow.

How to use it :
launchresizeandmove.cmd <width> <height> <x position> <y position> <executable> <args ...>


launchresizeandmove.cmd  1500 500 100 200  chrome.exe

to open Chrome at position 100x200 with a window size of 1500x500


<# :
:: Based on
:: which is based on
:: Converted to a batch/powershell hybrid via
@echo off
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF

Add-Type @"
  using System;
  using System.Runtime.InteropServices;

  public class Win32 {
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

$progname = $args[4]
$progargs = $args[5..($args.Count-1)]

$MyProcess = Start-Process -FilePath $progname -ArgumentList $progargs -PassThru
While ($MyProcess.MainWindowHandle -eq 0) {
Start-Sleep -Seconds 1

$h = $MyProcess.MainWindowHandle
# Set MoveWindow output = to $ret so that it doesn't print 'True' to the terminal
$ret = [Win32]::MoveWindow($h, $screen_x, $screen_y, $win_width, $win_height, $true )