CMD – Windows CMD Command
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Start a new CMD shell and (optionally) run a command/executable program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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.
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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"" |
I’d forever want to be update on new blog posts on this web site, saved to favorites! .