How to start a program with command line arguments on Windows' cmd with 'start' command?

36

8

I need to start a program (virtual machine) in the background with a start command on Windows' 7 command line. Normally you would do this like that:

start /b cmd yourprogram

But I need to pass some arguments and when I so it like this (without /b flag to see the debug information):

start C:\Users\USER>start "c:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -startvm "debian604 64"

I get this error message:

Windows cannot find '-startvm'. Make sure you typed the name correctly, and then try again.

On the other hand when I do it in the current command line window without the start in the beginning the virtual machine runs normally - but in the foreground.

Any solutions?

Patryk

Posted 2012-04-18T14:53:51.747

Reputation: 1 129

Answers

40

start /b "" "c:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -startvm "debian604 64"

If you read the parameter list with start /?:

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    command/program
                If it is an internal cmd command or a batch file then
                the command processor is run with the /K switch to cmd.exe.
                This means that the window will remain after the command
                has been run.

                If it is not an internal cmd command or batch file then
                it is a program and will run as either a windowed application
                or a console application.

    parameters  These are the parameters passed to the command/program.

It expects a title enclosed in quotes ("). Since your program path included quotes, it got interpreted as the title. Adding an explicit title (in this case, empty, "") works.


An alternative method is using the /d switch to specify the path. Specifically:

start /b /d "c:\Program Files\Oracle\VirtualBox\" VBoxHeadless.exe -startvm "debian604 64"

It appears to take the first argument after the /d switch as the path, even if it is quoted, and if the next argument is not quoted then this works. Everything after what is recognised as the command/program is passed as a parameter to that command/program. Note this will not work if the command/program has spaces in the name, e.g. VBox Headless.exe, since that would require quotes and be recognised as a title.


Overall, the first (explicit title) method is probably better. It was a bad design choice on the part of Microsoft, they really should have added a switch for title rather than "is the first argument enclosed in quotes?".

Bob

Posted 2012-04-18T14:53:51.747

Reputation: 51 526

Thanks very much for that :) Although as I see know start /b don't put the virtual machine in the background. I have to come up with something else then. – Patryk – 2012-04-18T16:45:22.403

If you just want to suppress output (stdout), add a >nul to the end. Use >nul 2>nul at the end to suppress both normal output and error (stderr) output. The command prompt window has to be kept open, however. – Bob – 2012-04-18T16:51:22.233

1@Patryk If you don't mind using PowerShell, this command will open a windowless process that isn't connected to the spawning (powershell.exe) process. Therefore the PowerShell window can be closed and VBoxHeadless will keep running. PowerShell comes with Windows 7. Start-Process -FilePath 'C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe' -ArgumentList '-startvm "debian604 64"' -WindowStyle Hidden – Bob – 2012-04-18T17:15:05.743

3

Actually the accepted answer is still not a solution. Closing the cmd window where the command was executed in will kill the vboxheadless process with the running virtual machine in it.

Using the approach below will make PowerShell run an independent process.

In cmd, run:

cd "c:\Program Files\Oracle\VirtualBox"
vboxmanage list vms

This will return something like:

"Webserver LAP" {8748b594-7e2d-4d8d-8785-999940766754}

Now take the UUID and run the following (still in cmd):

powershell start-process 'C:\program files\oracle\virtualbox\vboxheadless' '-s 8748b594-7e2d-4d8d-8785-999940766754' -WindowStyle Hidden

Thanks to the author of this article.

Charlie Vieillard

Posted 2012-04-18T14:53:51.747

Reputation: 131

WOW! cd SAVED MY HOURS! – T.Todua – 2014-09-15T10:56:24.993

In my comment on my own answer I've already provided an alternative method for "running VBox headlessly". My answer itself addresses the question of "passing arguments with start".

– Bob – 2013-10-23T12:18:35.207