54

I have an application which starts at position 0x0 of my desktop. I want to open it in the center of my desktop. I do not want to open it and use a move command to move it into center, instead my app should start immediately at center position.

Is there any way to do this via command prompt?

Andreas
  • 5,393
  • 9
  • 44
  • 53
Inside Man
  • 4,194
  • 12
  • 59
  • 119
  • 2
    Windows? Linux? Desktop environment? Language? Do you have the sources of the program? Please be more specific. – Anton Nov 02 '11 at 08:12
  • Hi Anton, I'm using windows. No I do not have the source, I have an animation creator, its output is exe, but I can not set window position for output exe and it always opens at 0x0 position. – Inside Man Nov 02 '11 at 10:31
  • 1
    @Sorpigal's answer may help; You may create a batch file that will launch the executable and move it's window with `cmdow`. – Anton Nov 02 '11 at 11:00
  • I published how to accomplish this in any easy way by using a generic batch script on [my blog](https://robsnotebook.com/batch-to-launch-an-application-at-desired-window-position-from-command-line/) – parallelsignal Sep 20 '20 at 15:48

9 Answers9

36

You'll need an additional utility such as cmdow.exe to accomplish this. Look specifically at the /mov switch. You can either launch your program from cmdow or run it separately and then invoke cmdow to move/resize it as desired.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
sorpigal
  • 25,504
  • 8
  • 57
  • 75
  • Thanks for reply, But can you give me the code for moving my app into center of desktop? – Inside Man Nov 02 '11 at 11:30
  • 1
    You'll have to make up your mind about this. If you don't want to use a utility then you'll actually have to write a program that uses MoveWindow(). – Hans Passant Nov 02 '11 at 11:59
  • Can you give an example? I can't see how to use cmdow with /mov – barlop Nov 17 '13 at 09:37
  • 1
    @barlop: `cmdow` doesn't support "center", you'll have to do the math yourself and pass the appropriate values to `/mov`. Did you want the formula for centering a box inside another box? – sorpigal Nov 17 '13 at 17:23
  • 1
    @Sorpigal I meant just the command line to use /mov but I got it now.. doing C:\>title abcdefg then from another window C:\>cmdow "abcdefg" /MOV 4 6 – barlop Nov 17 '13 at 17:37
  • @Sorpigal actually.. how would you do it using file handle? I can't seem to get the hex handle for a window. winspector I can't see it in there.. nor in process explorer. – barlop Nov 17 '13 at 17:49
  • @barlop. See my answer below – dgo May 16 '15 at 15:50
  • @user1167442 Thank you for that extraordinary way of pointing out to me that `cmdow /t` displays the handle alongside the image name, but it is good to know – barlop May 16 '15 at 23:50
  • 1
    if you type `cmdow /?` all will be revealed. – dgo May 17 '15 at 00:31
31

Have found that AutoHotKey is very good for window positioning tasks.

Here is an example script. Call it notepad.ahk and then run it from the command line or double click on it.

Run, notepad.exe
WinWait, ahk_class Notepad
WinActivate
WinMove A,, 10, 10, A_ScreenWidth-20, A_ScreenHeight-20

It will start an application (notepad) and then adjust the window size so that it is centered in the window with a 10 pixel border on all sides.

FuzzyWuzzy
  • 763
  • 1
  • 6
  • 12
  • Hi Fuzzy, Thanks for reply, What do you mean by "it is centered in the window with a 10 pixel border on all sides.". Is this code open my application in center of window on all computers with different resolution? – Inside Man Nov 02 '11 at 11:38
  • Take a look at the [WinMove](http://www.autohotkey.com/docs/commands/WinMove.htm) doc. The params X and Y are given the value 10, and the width and height are given the values: screen size minus 20. So it depends on how much space you want around the window. If you want to maximize the window instead use the [WinMaximize](http://www.autohotkey.com/docs/commands/WinMaximize.htm) function. – FuzzyWuzzy Nov 02 '11 at 11:44
  • @FuzzyWuzzy Great solution! Using AutoHotKey, I've created a script that open four instances of [VBALink emulator](http://www.vbalink.info) (for netplay), resize all windows in order for each of them to occupy 1/4 of the desktop, and positioned each window in a different corner of my screen (top left, top right, bottom left and bottom right). It worked like a charm! – Leonardo Montenegro Feb 26 '14 at 19:42
  • Hi Fuzzy,it's a good idea. I have thought about my situation, I want to use cmd.exe run a python script, move the cmd window to special position while the python script running. Saddly I lose to find a method. If I do as you post, when the command run to `WinMove`, the script already exit. Could pls give me more advice? It seems autohotkey not support multithread. Thanks. – roachsinai Aug 02 '18 at 17:22
  • USE FROM TRY "Window spy" TO EASLY IDENTIFY SEARCHED ahk_class – SkorpEN Dec 24 '18 at 06:03
26

I just found this question while on a quest to do the same thing.

After some experimenting I came across an answer that works the way the OP would want and is simple as heck, but not very general purpose.

Create a shortcut on your desktop or elsewhere (you can use the create-shortcut helper from the right-click menu), set it to run the program "cmd.exe" and run it. When the window opens, position it where you want your window to be. To save that position, bring up the properties menu and hit "Save".

Now if you want you can also set other properties like colors and I highly recommend changing the buffer to be a width of 120-240 and the height to 9999 and enable quick edit mode (why aren't these the defaults!?!)

Now you have a shortcut that will work. Make one of these for each CMD window you want opened at a different location.

Now for the trick, the windows CMD START command can run shortcuts. You can't programmatically reposition the windows before launch, but at least it comes up where you want and you can launch it (and others) from a batch file or another program.

Using a shortcut with cmd /c you can create one shortcut that can launch ALL your links at once by using a command that looks like this:

cmd /c "start cmd_link1 && start cmd_link2 && start cmd_link3"

This will open up all your command windows to your favorite positions and individually set properties like foreground color, background color, font, administrator mode, quick-edit mode, etc... with a single click. Now move that one "link" into your startup folder and you've got an auto-state restore with no external programs at all.

This is a pretty straight-forward solution. It's not general purpose, but I believe it will solve the problem that most people reading this question are trying to solve.

I did this recently so I'll post my cmd file here:

cd /d C:\shortucts
for %%f in (*.lnk *.rdp *.url) do start %%f
exit

Late EDIT: I didn't mention that if the original cmd /c command is run elevated then every one of your windows can (if elevation was selected) start elevated without individually re-prompting you. This has been really handy as I start 3 cmd windows and 3 other apps all elevated every time I start my computer.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Bill K
  • 62,186
  • 18
  • 105
  • 157
  • That's a clever solution, thanks. It seems like an acceptable hack but I think it would be a slight improvement to make a folder with several shortcuts with proper labels, like monitor0_upperleft, monitor0_upperight, and so on which could be reused as needed by simply calling them. Thanks again, much appreciated. – jacktrader Feb 21 '17 at 16:45
  • @jacktrader That's pretty much what I've been doing. You can even write a batch file to execute all the scripts in a given directory on startup. – Bill K Feb 21 '17 at 21:20
  • One thing I recently found, because automatic scripts with elevation was required (without saving credentials in a script).. is to set in the Local Security Policy, under Local Policies --> Security Options --> UAC (bottom set of policies) and set Run all administrators in Admin Approval Mode to "Disabled" and Behavior of the elevated prompt for ... "Elevate without prompting".. will set Windows to Auto Elevate if you are admin.. such a great find. Of course there are considerations to be made. Lol. Thanks again. – jacktrader Nov 29 '17 at 00:14
5

You can use nircmd project here: http://www.nirsoft.net/utils/nircmd.html

Example code:

nircmd win move ititle "cmd.exe" 5 5 10 10
nircmd win setsize ititle "cmd.exe" 30 30 100 200
nircmd cmdwait 1000 win setsize ititle "cmd.exe" 30 30 1000 600
Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
  • 1
    nircmd allowed me to center all active windows on a 4K monitor on a machine where I can't install PowerToys [FancyZones](https://learn.microsoft.com/en-us/windows/powertoys/fancyzones) `nircmd win setsize alltop 960 1 1920 2160` – Stefano Spinucci Mar 24 '21 at 21:09
4

This probably should be a comment under the cmdow.exe answer, but here is a simple batch file I wrote to allow for fairly sophisticated and simple control over all windows that you can see in the taskbar.

First step is to run cmdow /t to display a list of those windows. Look at what the image name is in the column Image, then command line:

mycmdowscript.cmd imagename

Here are the contents of the batch file:

:: mycmdowscript.cmd

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

SET IMAGE=%1
SET ACTION=/%2
SET REST=1
SET PARAMS=

:: GET ANY ADDITIONAL PARAMS AND STORE THEM IN A VARIABLE

FOR %%I in (%*) DO (
   IF !REST! geq 3 (
      SET PARAMS=!PARAMS! %%I
   )
   SET /A REST+=1
)

FOR /F "USEBACKQ tokens=1,8" %%I IN (`CMDOW /t`) DO (
     IF %IMAGE%==%%J (

     :: you now have access to the handle in %%I
     cmdow %%I %ACTION% !PARAMS!

     )
)

ENDLOCAL
@echo on

EXIT /b

example usage

:: will set notepad to 500 500

mycmdowscript.cmd notepad siz 500 500

You could probably rewrite this to allow for multiple actions on a single command, but I haven't tried yet.

For this to work, cmdow.exe must be located in your path. Beware that when you download this, your AV program might yell at you. This tool has (I guess) in the past been used by malware authors to manipulate windows. It is not harmful by itself.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
dgo
  • 3,877
  • 5
  • 34
  • 47
4

Bill K.'s answer was the most elegant if you just want to start a window at startup or start from a shortcut on the desktop.

Just open the window where you want it, right click and choose properties. select Layout uncheck "let system position window" and click OK.

Window will now open just where you want it. You can set font and window colors at the same time on other tabs. sweet.

GCD56
  • 51
  • 5
3

If you are happy to run a batch file along with a couple of tiny helper programs, a complete solution is posted here:
How can a batch file run a program and set the position and size of the window? - Stack Overflow (asked: May 1, 2012)

Community
  • 1
  • 1
2

I too wanted to do this and came across this thread: Positioning CMD Window. No external files to download as it creates a small bit of VBScript on the fly to do all the lifting. All you need to do is specify your X & Y coordinates in the following section: Cscript //nologo "%~DP0pos.vbs" "%~F0" 100 50. The .vbs script is also removed after it has been executed too so there is no need to tidy anything up.

Place this at the top of your batch file:

REM - Position the CMD Window Using .VBS -----------------------------------------
REM == MUST BE AT The Begining of The Batch =========
   IF "%~1" == "RestartedByVBS" Goto :Code

   REM Create the VBScript, if not exist
   IF NOT EXIST "%~DP0pos.vbs" (
      (FOR /F "tokens=1*" %%a in ('findstr "^VBS:" ^< "%~F0"') do (
         echo(%%b
      )) > "%~DP0pos.vbs"
   )
   REM Start "" "%~DP0pos.vbs" "%~F0" 100 50
   Cscript //nologo "%~DP0pos.vbs" "%~F0" 100 50
   EXIT /B
:code
DEL /Q "%~DP0pos.vbs"
REM ------------------------------------------------------------------------------

PLACE THE CONTENTS OF YOUR OWN BATCH FILE HERE

And this at the bottom:

REM - Position the CMD Window Using .VBS -----------------------------------------
:Pos <BatchFileName> <X_Coordinate> <Y_Coordinate>

REM This Function will take three inputs: the name of the Batch file to execute
REM and the X and Y Coordinates to Position its CMD window

VBS: Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
VBS: Set objConfig = objWMIService.Get("Win32_ProcessStartup")
VBS: objConfig.SpawnInstance_
VBS: objConfig.X = WScript.Arguments(1)
VBS: objConfig.Y = WScript.Arguments(2)
VBS: Set objNewProcess = objWMIService.Get("Win32_Process")
VBS: intReturn = objNewProcess.Create( chr(34) & WScript.Arguments(0) &chr(34)& " RestartedByVBS", Null, objConfig, intProcessID)
REM ------------------------------------------------------------------------------

Enjoy :)

Bri
  • 59
  • 6
1

Thanks To FuzzyWuzzy , set the following code ( Quick & Dirty Example for 1920x1080 screen resolution - without automatic width and height calculation or function use etc ) in AutoHotKey to achive the following : enter image description here

v_cmd = c:\temp\1st_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
SetTitleMatchMode 2
SetTitleMatchMode Fast
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, 0, 0,1920,500

v_cmd = c:\temp\2nd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, 0, 500,960,400

v_cmd = c:\temp\3rd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, 960, 500,960,400

SMALL EDIT same code with Auto X / Y screen size calculation [ 4 monitors ], yet, can be used for 3 / 2 monitors as well.

Screen_X = %A_ScreenWidth%
Screen_Y = %A_ScreenHeight%

v_cmd = c:\temp\1st_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
SetTitleMatchMode 2
SetTitleMatchMode Fast
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, 0, 0,Screen_X/2,Screen_Y/2

v_cmd = c:\temp\2nd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, Screen_X/2, 0,Screen_X/2,Screen_Y/2

v_cmd = c:\temp\3rd_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, 0, Screen_Y/2,Screen_X/2,Screen_Y/2

v_cmd = c:\temp\4th_Monitor.ps1
Run, Powershell.exe -executionpolicy remotesigned -File %v_cmd%
WinWait, PowerShell
Sleep, 1000
    ;A = Active window - [x,y,width,height]
WinMove A,, Screen_X/2, Screen_Y/2,Screen_X/2,Screen_Y/2