Skip to main content

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:

CommandDescription
dirLists files and directories
cdChanges the current directory
copyCopies a file
moveMoves a file
delDeletes a file
mkdirCreates a directory
rmdirDeletes an empty directory
typeDisplays the content of a text file
echoDisplays text
setDisplays or sets environment variables
clsClears 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 CommandMeaning of the option
dir /wDisplay files and directories in wide format (columns).
dir /pDisplay files page by page, pausing.
copy /yCopy a file without asking for confirmation to overwrite.
del /qDelete files without confirmation.
taskkill /IM notepad.exeTerminate 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 .bat file.
  • 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.