Starting Command Prompt and Running a Command
The /C and /K command-line arguments allow you to start a Command Prompt session and immediately run a command or program. The difference between the two is that Cmd /C commandstring terminates the Command Prompt session as soon as commandstring has finished, whereas Cmd /K commandstring keeps the Command Prompt session open after commandstring has finished. Note the following:
You must include either /C or /K if you want to specify a command string as an argument to Cmd. If you type cmd commandstring, the command processor simply ignores commandstring.
While commandstring is executing, you can't interact with the command processor. To run a command or program and keep the Command Prompt window interface, use the Start command. For example, to run Mybatch.bat and continue issuing commands while the batch program is running, type
cmd /k start mybatch.batIf you include other command-line arguments along with /C or /K, the /C or /K must be the last argument before commandstring.
Cmd.exe and Other Command Prompts
Cmd.exe, the application whose name is Command Prompt, is only one of several forms of command prompt available in Windows. Others include the Run command (an optional item on the Start menu, which you can get to by pressing Windows logo key+R), the Address toolbar, the address bar in Windows Explorer, and even the address bar in Internet Explorer. In many ways, these command prompts function alike. You can start a Windows-based application from any of them, for example. (If you start from Internet Explorer, you need to include an explicit path specification, and you might need to answer some security prompts.) What's exceptional about Cmd.exe is that it allows you to execute internal MS-DOS-style commands-that is, commands that are not stored in discrete .exe files.
Using AutoRun to Execute Commands When Command Prompt Starts
Command Prompt's equivalent to the old MS-DOS Autoexec batch mechanism is a feature called AutoRun. By default, Command Prompt executes on startup whatever it finds in the following two registry values:
The AutoRun value in HKLM\Software\Microsoft\Command Processor
The AutoRun value in HKCU\Software\Microsoft\Command Processor
The AutoRun value in HKLM affects all user accounts on the current machine. The AutoRun value in HKCU affects only the current user account. If both values are present, both are executed-HKLM before HKCU.
Both AutoRun values are of data type REG_SZ, which means they can contain a single string. (You can enter a multistring value, but Windows ignores all but the first string.) To execute a sequence of separate Command Prompt statements, therefore, you must use command symbols or store the sequence as a batch program, and then use AutoRun to call the batch program.
To specify an AutoRun value, open a registry editor and navigate to the Command Processor key in either HKLM or HKCU. Create a new string value there, and name it AutoRun. Then specify your command string as the data for AutoRun, exactly as you would type it at the command prompt.
To disable AutoRun commands for a particular Command Prompt session, start Cmd with /D. For more about Command Prompt's command-line syntax, see the next section.