CMD/DOS Essentials

CMD/DOS Essentials

CMD/DOS - The Essentials

First: The "Command Prompt", as it is officially known in Windows, is the legacy CLI (though it isn't going anywhere) provided by CMD.EXE which resembles the MS-DOS Prompt (provided by COMMAND.COM). CMD.EXE provides some built-in commands, and others are external .EXE commands - but we'll consider anything provided my Microsoft in MS Windows to be "built-in".

The CMD Command Prompt aka DOS Prompt carries a lot of legacy baggage, in fact most of the commands are made to operate identically to how they worked in MS-DOS, but with some enhancements. This is both a blessing and a curse.

There's less uniform standardization and consistency with "DOS" commands (even ones provided by Microsoft with Windows, due to legacy reasons) but even less so with random Console .EXE files you may download and run. (though many are trying to follow modern Linux/Bash/macOS standards)

To start the Command Prompt run cmd.exe, or look for "Command Prompt" in your Start menu.

Type commands at the C:\> prompt and press Enter after each.

CMD.exe Command Prompt example

In the example above the command cd "some folder" was typed at the C:\> prompt, the prompt changed to show the working directory was changed to the "some folder" directory, a valid command was typed, the results came back (showing where the cmd.exe file is located), and then an unknown command was tried - resulting in the error message which says exactly that - the command wasn't recognized. Now the C:\some folder> prompt waits for the next command.



Commands and Options

Included Windows console commands (both CMD built-in commands and external .EXE commands) are often short (dir instead of "list directory" or md instead of "make directory") and sometimes cryptic (such as qwinsta instead of "query windows station"); often because these commands have been around since the days of MS-DOS and 8 character filename limits. Generally there is no regular pattern all command names follow.

Parameters/switches/options for CMD/DOS Commands are separated from commands with a Space and usually a single dash - or slash /, as in -AH or /B.

The main point is: Every command can be different so check the help text!



Getting Help

Getting Help for CMD/DOS commands can also vary. A short list of the basic included Windows console commands can be found by running help but this by no means includes them all (mostly just the older legacy MS-DOS derived commands).

Usually you need to pass a switch or option for DOS/CMD/Console .EXE commands to show their Help text. It can vary, but one of these should work:

If the Help Text is too long add | more after, as in: chkdsk /? | more. But don't just type the name of the command with no switches to see what it may do until you've tried /?, /h, etc. because some commands will do something even with no options/switches. For example: IISRESET will restart all IIS Web Services if you provide no options, which may not be what you want/expect! iisreset /? will show the help.

Sometimes you can just type the command with no options and it will provide help, but sometimes the command doesn't require options and it just runs!
If this happens and it appears to hang (likely waiting for input) try pressing any of: Ctrl+C, Ctrl+Z, or Ctrl+D to either cancel the command or end the input.

While the meaning of console CLI switches/options/arguments can vary usually how they are represented in Help Text is fairly standard.

CMD example help text from: copy /?

In the example above, copy /? shows there is only one required parameter, the source as it is not enclosed in [square brackets]. The other switches and options (such as /D, /V, etc) are in [square brackets] so they are optional, as is the destination.
If options are separated by a vertical pipe | character that means you can use only one of those options (either /A or /B). Then there is usually a list of what each option/switch means.
Whether you can specify options in lower case or use - instead of / depends on the command, sometimes if there isn't a /B and a /b option you can use either, other times not, other times they could be different options entirely!



Also worth knowing

There's Tab completion only for files/directories at the CMD Command Prompt. Type a command and if you need to specify a file/folder start typing the name and press the Tab key to cycle through all matching files/folders. For example: type cd \win[press TAB] and it will complete to cd C:\Windows

Another trick you can sometimes use: the * wildcard. If it will match only one folder, as in cd \win*, it will resolve to the first folder starting with "win" in the root of the drive.

The Command Prompt is (generally) NOT Case Sensitive. You can type DIR, dir, DiR, etc.

To run a script you can just run it as any other built-in or external .EXE command, so long as it is in the PATH it will be found.

You can enter any CMD/DOS command at the prompt and test/build scripts one command at a time - that is: variables and whatnot persist between commands (but not between sessions - if you close the window everything not saved to a file is gone). However the Shell itself only has limited expression evaluation abilities, you can can't just type 2 + 2 and get back 4 like you can with PowerShell.

CMD.EXE is a Windows program, so be mindful of Run as Administrator, Admin Rights, UAC, etc. If you can't do something, try running your Command Prompt as an Administrator.