CMD – Windows CMD Command

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

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.

In Windows Explorer, you can type “cmd” in the address bar to open a prompt at the current location.
For more detail about the CMD shell: QuickEdit, handing quotes, max line length, etc, see the CMD Syntax page.

ErrorLevel

CMD /C will return an error level, 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

Alternatively, start the batch file with pushed “%~dp0”
That will change directory to your batch file location (%0), and for UNC paths it will auto-create a temporary drive map.

Launching CMD/batch files from PowerShell

When 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

Anything to the left of –% will be expanded:

$folder = ‘C:\financial $accounts\’
cmd.exe /c dir $folder –%/w

Inheritance

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

In all these cases, 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.
For more detail on inheritance and expansion see this StackOverflow thread.

Command Extensions

Much of the functionality of CMD.exe can be disabled – this will affect all the internal commands, and most dynamic/volatile environment variables (%TIME%, %DATE%, %RANDOM%, %CD%, etc)

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)

You may also like...

1 Response

  1. zortilo nrel says:

    I’d forever want to be update on new blog posts on this web site, saved to favorites! .

Leave a Reply

Your email address will not be published.