DEV Community

Victor James
Victor James

Posted on • Originally published at windowscmd.com on

CMD

This CMD command is to start a new CMD shell.

CMD Line

CMD Line is a text-based interface that transfers the command from the user to the OS.

CLI-Command Line Interpreter

The command-line interpreter (CLI) for windows is CMD.EXE. Also, you can use PowerShell which can automize many system administration tasks and these tasks are not approachable in command prompt. With the help of the CLI, you can communicate with your system by typing the text in commands. As a result, your system answers to the text that you have typed previously, and then the user can proceed with the next command line that shows up.

To open the Run command Window

Run command window helps you to run programs, open the files and folders. And there are several ways to open up the run command dialog box. This run command box is useful for you to head straight to the destined program easily.

By using the Keyboard shortcut Key:

WINDOWS+R

The shortcut key method to the run command box is the most commonly used one. Also, this method is in all versions of Windows. You need to press the Windows+ R key concurrently to open the Run command dialog box.

Run command box
Run command box

How does the CMD command work?

This windows command-line tool is to start a new CMD shell. This command starts a new shell and optionally runs a command or executable program.

Syntax


      CMD [charset] [options]

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

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

Enter fullscreen mode Exit fullscreen mode

Available options:

  • /CRuns Command and then terminate.
  • /K – Run Command and then returns to the CMD prompt. This is helpful for testing, to analyze variables
  • Command – The command , program or batch script to be run.
  • /T:fg – This Sets the foreground/background colours.
  • /A – Represents the output ANSI characters.
  • /U -Output UNICODE characters (UCS-2 le).
  • /D – Ignore registry Auto Run commands. HKLM | HKCU \Software\Microsoft\Command Processor\AutoRun.
  • /E:ONEnable CMD Command Extensions (default)
  • /X – Enable CMD Command Extensions (old switch for compatibility)
  • /E:OFFDisable CMD Command Extensions
  • /Y – Disable CMD Command Extensions (old switch for compatibility)
  • /F:ONEnable auto-completion of path names entered at the CMD prompt
  • /F:OFFDisable auto-completion of path names entered at the CMD prompt (default).

  • In the command prompt

    • CTRL+D gives folder name completion.
    • Ctrl+F gives file name completion.
  • /Q – To turn echo off.

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

  • /V:ONEnable 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:OFFDisable delayed environment expansion. Delayed Environment expansion can also be set with SETLOCAL.

In the new shell, the remainder of the command line is processed as an immediate command if /C or /K is specified.

Multiple commands are divided by the command separator ‘& ‘ or ‘&& ‘ are accepted if surrounded by quotes.

In Windows Explorer, you can type “ cmd ” in the address bar to open a prompt at the current location.

Errorlevel

  • %errorlevel% = 1 when the drive does not exist
    • For example, CMD /C will return an error level if the drive Z: does not exist

CMD /c dir Z:

Enter fullscreen mode Exit fullscreen mode

Introducing CMD/batch files from a UNC path

Launching a batch file from a UNC path will inherently run CMD.exe with that UNC path, this will often return the error as

path is an invalid current directory path. UNC paths are not supported. Defaulting to Windows directory.

It can be dangerous if the batch file makes any assumptions about the current directory, For instance: if at all, the line contains DEL *.ico it will delete .ico files from the Windows directory rather than the folder where the batch file resides.

  • If you are sure that the batch file won’t be affected by this, you can rectify the error in two ways,
    • Add a CLS command as the first line of the batch script.
    • Add the registry key DisableUNCCheck as described in Q156276.
    • Instead, start the batch file with pushd “%~dp0”.

Launching CMD/batch files from PowerShell

While calling CMD from PowerShell the –% operator can be used to control when $ symbols and quoted expressions will be interpreted/expanded by PowerShell:


cmd.exe --% /c dir "C:\financial $accounts\" /w

Enter fullscreen mode Exit fullscreen mode

Things after the –% will be extended


$folder = 'C:\financial $accounts\'
cmd.exe /c dir $folder --%/w

Enter fullscreen mode Exit fullscreen mode

Inheritance

A new CMD.exe session can be represented in several ways, explicitly beginning a new CMD session from an existing CMD shell, CALLing a batch file, or implicit representation caused by piping a command or running a FOR /F command.

Only the environment variable values are inherited by the new CMD session. Delayed expansion state, Command extension state, and Echo state will all revert back to the default state based on the registry.

Command extensions

CMD.exe can be disabled, but it affects all the internal commands and most dynamic/volatile environment variables.

By default, Command Extensions are enabled.

It can be changed by setting a value in the registry:


HKCU\Software\Microsoft\Command Processor\EnableExtensions

Enter fullscreen mode Exit fullscreen mode

Command extensions can be turned off by running


 CMD /e:on or CMD /e:off

Enter fullscreen mode Exit fullscreen mode

Or by running


EnableExtensions

Enter fullscreen mode Exit fullscreen mode

Examples:

To run a program and pass a Filename parameter


CMD /c write.exe c:\docs\sample.txt

Enter fullscreen mode Exit fullscreen mode

In order to run a program and pass a Long Filename


CMD /c write.exe "c:\sample documents\sample.txt"

Enter fullscreen mode Exit fullscreen mode

Spaces in Program Path


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

Enter fullscreen mode Exit fullscreen mode

For spaces in Program Path + parameters


CMD /c ""c:\Program Files\demo.cmd"" Parameter1 Param2

Enter fullscreen mode Exit fullscreen mode

Spaces in Program Path + parameters with spaces


CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""

Enter fullscreen mode Exit fullscreen mode

The command will strip the outer quotes if there is a double quote at the start and end, so you can also cheat and write the above as,


CMD /k @"c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space"

Enter fullscreen mode Exit fullscreen mode

To launch Demo1 and then Launch Demo2


CMD /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""

Enter fullscreen mode Exit fullscreen mode

Verdict:

In this, we have seen about the CMD windows command-line tool, which is used to start a new CMD shell. If you have any suggestions or queries regarding this article, let us know in the comment section.

Read further:

  • CD This windows command-line tool changes the directory and moves to the specific folder. CHDIR is the equivalent word of CD.
  • CMD CMD windows command is to start a new shell in a command or executable program.
  • Attrib The Attrib command-line tool is to display or change the file attributes.
  • CHDIR The CHDIR command Changes the Directory and moves to the specific folder.
  • CLS CLS command is to erase all characters and graphics from the screen. To be more precise, this command clears the screen.

The post CMD appeared first on Windows Commands- SS64 Commands.

Discussion (0)