CMD.exe

Start a new CMD shell and (optionally) run a command/executable program.

Syntax
      CMD [charset] [options]

      CMD [charset] [options] [/C Command] 

      CMD [charset] [options] [/K Command] 

Options   
   /C     Run Command and then terminate

   /K     Run Command and then return to the CMD prompt.
          This is useful for testing, to examine variables

   Command : The command, program or batch script to be run.
             This can even be several commands separated with '&' 
             (the whole should also be surrounded by "quotes")

   /T:fg  Sets the foreground/background colours 
   
   /A     Output ANSI characters
   /U     Output UNICODE characters (UCS-2 le)
          These options will affect piping or redirecting to a file.
          Most common text files are ANSI, use these switches
          when you need to convert the character set.

   /D     Ignore registry AutoRun commands
          HKLM | HKCU \Software\Microsoft\Command Processor\AutoRun

   /E:ON  Enable CMD Command Extensions (default)
   /X     Enable CMD Command Extensions (old switch for compatibility)
   /E:OFF Disable CMD Command Extensions
   /Y     Disable CMD Command Extensions (old switch for compatibility)

   /F:ON  Enable auto-completion of pathnames entered at the CMD prompt
   /F:OFF Disable auto-completion of pathnames entered at the CMD prompt (default)

At the command prompt Ctrl-D gives folder name completion and Ctrl-F gives File and folder name completion.

These key-strokes will display the first matching path. Thereafter, repeated pressing of the same control key will cycle through the list of matching paths. Pressing SHIFT with the control key will move through the list backwards.

   /Q    Turn echo off

   /S    Strip " quote characters from command.
         If command starts with a quote, the first and last quote chars in command
         will be removed, whether /s is specified or not.

   /V:ON Enable delayed environment variable expansion 
         this allows a FOR loop to specify !variable! instead of %variable% 
         expanding the variable at execution time instead of at input time. 
   
   /V:OFF
         Disable delayed environment expansion.
         Delayed Environment expansion can also be set with SETLOCAL

If /C or /K is specified, then the remainder of the command line is processed as an immediate command in the new shell. Multiple commands separated by the command separator '&' or '&&' are accepted if surrounded by quotes.

For more details on working in the CMD shell: QuickEdit, handing quotes etc, see the CMD Syntax page.

ErrorLevel

CMD /C will return an errorlevel, for example CMD /c dir Z: where the drive Z: does not exist, will return %errorlevel% = 1 to the calling CMD shell.

Launching CMD/batch files from a UNC path

While most command line utilities do fully support UNC paths, they cannot be set as the current directory. Launching a batch file from a UNC path will implicitly run CMD.exe with that UNC path, this will often return the error: path is an invalid current directory path. UNC paths are not supported. Defaulting to Windows directory.
This can be dangerous if your batch file makes any assumptions about the current directory,
e.g. if it includes the line DEL *.ico, that will delete .ico files from the Windows directory instead of the folder where the batch file resides.
If you are confident that the batch file won't be affected by this, you can suppress the error in one of two ways: Add a CLS command as the first line of the batch script, or add the registry key DisableUNCCheck as described in Q156276

Command Extensions

Much of the functionality of CMD.exe can be disabled - this will affect all the internal commands, Command Extensions are enabled by default. This can be changed by setting a value in the registry: HKCU\Software\Microsoft\Command Processor\EnableExtensions
Command Extensions can also be turned on or off by running CMD /e:on or CMD /e:off
or run SETLOCAL EnableExtensions (SetLocal will take precedence)

The /knetdiag switches are undocumented and work in Windows XP only, they list and may fix several networking issues.
 /knetdiag /debug
 /knetdiag /fix

Examples:

Run a program and pass a Filename parameter:
CMD /c write.exe c:\docs\sample.txt

Run a program and pass a Long Filename:
CMD /c write.exe "c:\sample documents\sample.txt"

Spaces in Program Path:
CMD /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""

Spaces in Program Path + parameters:
CMD /c ""c:\Program Files\demo.cmd"" Parameter1 Param2

Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""

Launch Demo1 and then Launch Demo2:
CMD /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""

“Those who can command themselves, command others” - Hazlitt

Related:

EXIT - Use this to close a CMD shell and return.
CALL - Call one batch program from another
START - Run a program, command or batch file
DOSKEY - Edit command line, recall commands
CMD Internal - Commands that are Internal to the CMD shell.
Q156276 - Cmd does not support UNC names as the current directory
Powershell: You may run the CMD shell under Powershell, Exit will return you to the PS prompt.
Equivalent bash command (Linux): bash - run the bash shell (also csh, ksh, sh)


© Copyright SS64.com 1999-2015
Some rights reserved