CMD Command Prompt Boot Camp
This section will be a mini boot-camp / accelerated crash course on using the CMD/DOS Command Prompt. It won't cover basic file system navigation, I assume you can complete the CLI Task Test, if not run through Zen's CLI Crash Course first. It won't be as long as the PowerShell Boot Camp, because beyond a few specific cases: you should just focus on learning PowerShell instead. Enough to quickly get up to speed at the CMD Prompt and know what to Google for if you get stuck. For the basics see the CMD/DOS Essentials page.
Most commands use slash: / or dash: - to start options/switches, as in /ah
or -a
. Most commands support /?
to get help, as in
dir /?
. When you don't know what options to use, try that first. For more info on CMD Help see the Essentials page.
The CMD/DOS Command Line supports the pipeline using the vertical pipe character: | but chaining together multiple commands always easy. Common ones are:
more
to show text one screen-full at a timerobocopy /? | more
will show the Help Text and pause after each pagemore
to display a file a page at a time:
more C:\Windows\debug\WIA\wiatrace.log
type
, as in:
type C:\Windows\System32\WimBootCompress.ini
find
or findstr
to filter and show only matching [or not matching] lines. findstr
supports RegEx for more complex searching/filteringfindstr /L /I "Successfully "c:\logfiles\*.txt"
to search a bunch of .TXT files for the word
"Successfully".sort
to sort the lines of the output, for example:type randomLines.txt | sort > sortedLines.txt
and capture the output with redirection.clip
to copy the text output to the Windows Clipboard:type c:\temp\logfile.txt | clip
and the contents of the file would be in the ClipboardsomeCommand.exe list -option %windir% /switch --etc | find /I "value" | sort | more
cut
,
tail
, tr
, sed
, uniq
, etc: UNIX/Linux commands that have been ported to
Windows. You can find some at GNUWin32
(they are not built in to Windows). But unless you're familiar with Linux CLI already, it's easier to to learn
PowerShell. In CMD/DOS everything is plain-text, which may initially make things seem easier but eventually it's harder and less
accommodating of changes. PowerShell is all Objects.
Command redirection using the greater than symbol > can be helpful, if you add >
to create a file, or >>
to append to a file, you can
capture the output of any command. Such as: dir c:\windows > c:\winfiles.txt
and then perhaps edit the file
in Notepad (eg. to remove some header lines) before further processing it.
If you're not sure what the command you're looking for is, you can list the basic built-in CMD commands with help
,
and you can search for external (included or downloaded) .EXE file with where.exe
, including using wildcards. So
you can search for any .EXE that includes "exec" with where.exe *exec*.exe
- which might show the MSIEXEC
and PSEXEC.EXE
(if you had it installed). So searching for where.exe *dfs*.exe
would show any DFS related commands.
Note: Where
(without .exe) is also a built-in PowerShell command, so using .exe with where.exe
will let you use it in PowerShell too.
CMD has environment variables which you can see with the SET
command, and reference them with percent signs
% on either side, such as %windir%
or %USERNAME%
to
reference the Windows directory (ie. c:\windows) or the current logged on username in commands and scripts.
You can use set
to not just display but also add or change an environment variable (within the lifetime the
Console window stays open, such as set location=c:\temp\folder
and then reference %location%
later on such as copy *.* %location%
would resolve to
copy *.* c:\temp\folder
. Using environment variables can make commands and scripts more flexible by either
using system defined values (which can change) or only having to update a variable in one place.
You can make a Batch Script by pasting all the lines you want to run into a plain text file and saving it as a .BAT file, then running it is like typing those commands at the prompt, one line at a time - making it easy to carry out the same set of commands over and over, or on different systems.
If you need to run a .BAT script or a CMD built-in command (such as dir
or copy
) from some "run a
command" process you can do so using cmd /c script.bat
or cmd /c dir
and CMD.exe will run the
script/command and then exit.
CMD also support conditional branching (IF
) and looping (FOR
, GOTO
or
CALL
) but it really isn't worth learning to write complex scripts for CMD processing - too much arcane legacy
baggage and strange syntax. If you need to write something more complex: Use PowerShell -
it's included in all supported Windows OS, can be installed on older OS, and is much more flexible, regular, and friendly.