Introduction to CMD
What is a terminal?
A terminal is a user interface that allows interaction with a computer system (operating system) via textual commands. Unlike a graphical user interface (GUI), a terminal receives commands manually entered by the user and returns text in response.
Terminals generally offer:
- Direct access to system management.
- Automation of repetitive tasks.
- Quick system troubleshooting.
History and differences between CMD and PowerShell
Windows offers two main command interpreters: CMD and PowerShell.
CMD
CMD (Command Prompt), also called "Command Prompt" in French, is the standard command interpreter on Windows since MS-DOS.
- Origin: directly derived from MS-DOS.
- Simple language based on predefined commands.
- Ideal for basic scripts and backward compatibility.
PowerShell
PowerShell is a modern shell developed by Microsoft to address the shortcomings of CMD.
- Origin: Introduced with Windows Vista in 2006.
- Based on .NET Framework, supporting complex and object-oriented commands.
- Uses cmdlets (command-lets) for increased flexibility.
Why choose CMD or PowerShell?
- CMD: simple scripts, quick compatibility, and basic tasks.
- PowerShell: complex tasks, advanced automation, and system administration.
CMD: Syntax and essential commands
General syntax
command [options] [arguments]
Frequent commands
Here is a list of the most common commands in CMD:
| Command | Description |
|---|---|
dir | Lists files and directories |
cd | Changes the current directory |
copy | Copies a file |
move | Moves a file |
del | Deletes a file |
mkdir | Creates a directory |
rmdir | Deletes an empty directory |
type | Displays the content of a text file |
echo | Displays text |
set | Displays or sets environment variables |
cls | Clears the screen |
Examples
List files:
dir
Change directory:
cd C:\Users\Haythem\Documents
Copy a file:
copy fichier1.txt fichier2.txt
Options in CMD: Using the / character
Unlike Unix/Linux terminals (like bash) where command options are indicated by a dash - or double dash --, CMD traditionally uses the slash / character.
For example, in Bash (Linux or Mac):
ls -l -a
But in CMD (Windows):
dir /w /p
Example of CMD commands with options:
| CMD Command | Meaning of the option |
|---|---|
dir /w | Display files and directories in wide format (columns). |
dir /p | Display files page by page, pausing. |
copy /y | Copy a file without asking for confirmation to overwrite. |
del /q | Delete files without confirmation. |
taskkill /IM notepad.exe | Terminate a specific application (IM means Image Name). |
Getting help on commands (/ ?)
Unlike Unix or Bash, where the --help option provides help on a command, the equivalent in CMD is to use the /? option.
General syntax:
command /?
This command displays a detailed description as well as the different options available for a specific command.
Example:
To get help for the dir command, type:
dir /?
CMD Variables
A CMD variable is a temporary reference allowing storage of information or values that can be used later in the same CMD session or in a batch script.
- Syntax:
set variable_name=value
- Simple example:
set utilisateur=Haythem
echo Bonjour %utilisateur%
By default, variables defined with the set command in a CMD instance are not visible in another simultaneously opened instance. Each CMD instance maintains its own variables independently of others. To create a permanent environment variable (which will be visible to all new CMD sessions), you must use the Windows control panel or the setx utility:
setx utilisateur Haythem
Batch scripts (.bat)
Batch files allow automation of repetitive tasks.
Complete example:
Create a file named backup.bat:
@echo off
echo Starting backup...
set source=C:\Users\Haythem\Documents\Important
set destination=D:\Backup\Documents\Important
if not exist %destination% mkdir %destination%
copy %source%\*.txt %destination%
echo Backup completed.
pause
Running batch scripts
- Double-click on the
.batfile. - From CMD:
backup.bat.
References
There are many resources to learn how to write batch scripts, including the official Microsoft documentation and other online tutorials like tutorialspoint.
Advanced manipulation with CMD
Redirection
>: sends output to a file.>>: appends to an existing file.
Example:
dir > liste_fichiers.txt
Pipes (|)
Allows sending the output of one command to another.
dir | find "rapport"
Task management
List processes:
tasklist
Terminate a process:
taskkill /IM notepad.exe
Network management
Display network information:
ipconfig
Check network connection:
ping google.com
Conclusion
CMD is an essential tool for Windows users looking to quickly automate simple tasks. Despite its limitations compared to PowerShell, its simplicity and speed make it a good choice for common tasks and simple scripts.