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 ...>
Example
launchresizeandmove.cmd 1500 500 100 200 chrome.exe https://rgagnon.com/howto.html
to open Chrome at position 100x200 with a window size of 1500x500
[launchresizeandmove.cmd]
<# : :: Based on https://robsnotebook.com/batch-to-launch-an-application-at-desired-window-position-from-command-line/ :: which is based on https://gist.github.com/coldnebo/1148334 :: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780 @echo off setlocal set "POWERSHELL_BAT_ARGS=%*" if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%" 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 { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); } "@ $win_width=$args[0] $win_height=$args[1] $screen_x=$args[2] $screen_y=$args[3] $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 )