Disabling unnecessary services in Windows : Services list and guide
How do I create a batch file (.bat or .cmd) under Windows

How do I create a batch file (.bat or .cmd) under Windows?

Batch files (.bat), which first appeared with the first versions of MS-DOS, are one of the oldest ways of automating tasks under Windows. Even today, they are still widely used for repetitive actions, systems management and software deployment. Thanks to their simplicity and compatibility with almost all versions of Windows, they are an indispensable tool for administrators and advanced users alike. In this article, we’ll explore how to create and run a batch file, as well as some basic commands to get you started.

What is a Batch file?

A Batch file (or Batch script) is a text file containing a series of commands executed sequentially by the Windows command interpreter (cmd.exe). These files are mainly used to automate repetitive tasks without manual intervention.

Historically introduced under MS-DOS, Batch files are still a simple way of executing system instructions, despite the emergence of more modern languages such as PowerShell. They are particularly appreciated for their lightness and compatibility with all versions of Windows.

The main purpose of a batch file is to automate operations such as :

  • Execute multiple commands in a single action,
  • File and folder management (create, delete, copy),
  • System administration (shutdown/restart, process management),
  • Automatic software launch,
  • Installing or configuring programs on multiple machines.

Tutorial: Creating a batch file in Windows

Batch files allow you to automatically execute a series of commands in Windows. They are particularly useful for automating common tasks such as displaying messages, managing files or launching programs. In this tutorial, we’ll look at how to create a batch file and execute it, with a simple example using the echo command.

A batch file is simply a text file containing commands that Windows can execute. It has a .bat or .cmd extension instead of .txt. When you double-click on it, Windows follows the instructions it contains and displays the result in a black window called Command Prompt (cmd.exe).

  1. Click on Start, type Notepad and open it.
  2. Enter the following code:
@echo off echo Hello! This is a batch file. pause
  1. Click on File > Save as…

Choose a location (e.g. Office).

  1. In File name, enter MonScript.bat
  2. In Type, select All files (.) instead of Text files (.txt).
  1. Click on Save.

Explanation of code lines:

  • @echo off: Prevents commands from being displayed, so that only the result is shown.
  • echo Hello! This is a batch file. Displays the message.
  • pause: Pauses the program until the user presses a key.

Run the Batch file

  1. Go to the location where you saved the file (e.g. Desktop).
  2. Double-click on MonScript.bat.
  3. A black window (the Windows Command Prompt) opens, displaying :
Launch a .bat script from the command prompt (CMD).
Launch a .bat script from the command prompt (CMD).
  1. Press any key to close the window.

Batch file example and use case

Batch files are used in many contexts, from personal use to enterprise systems administration. Here are just a few examples:

Automation of common tasks

Deleting temporary files:

del /q /s C:Temp*

Open several programs with a single command :

start notepad.exe start chrome.exe

Restarting a PC after a set time :

shutdown -r -t 60

Batch files are a powerful tool for Windows automation, but they also have limitations that may require the use of other languages such as PowerShell or Python.

The main Batch commands for Windows

Some commands require administrator rights to run. To run a Batch file as administrator :

  1. Right-click on the .bat file.
  2. Select Run as administrator.
CommandDescriptionExample
echoDisplays a message on the screen.echo Hello, this is a message.
@echo offDisables display of executed commands.@echo off
pausePauses the script until a key is pressed.pause
clsClears the Command Prompt screen.cls
cdChanges directory.cd C:Folder
md / mkdirCreates a new folder.mkdir C:NewFolder
delDeletes a file.del C:File.txt
copyCopies a file or several files.copy fichier.txt C:Backup
moveMoves a file or folder.move fichier.txt C:NewFolder
tasklistDisplays a list of current processes.tasklist
taskkillCloses a running process.taskkill /IM notepad.exe /F
shutdownShuts down or restarts the computer.shutdown -s -t 60
pingTests connection to an IP address or site.ping google.com
ipconfigDisplays network configuration.ipconfig /all
netstatDisplays active network connections.netstat -an
tracertDisplays the network path to an address.tracert google.com
startOpens a program or file.start notepad.exe
titleChanges Command Prompt title.title My Batch Script
colorChanges text and background color.color 0A
setSets a variable.set name=Jean
ifExecutes a command conditionally.if "%name%"=="John" echo Hello John!
gotoRedirects to a specific section of the script.goto :end
exitCloses the script or Command Prompt.exit

The limits and evolution of scripting under Windows

Batch files have long been one of the main ways of automating tasks under Windows. However, as administration and automation needs have evolved, other more powerful and flexible solutions have emerged. These include PowerShell, Python scripting and various third-party tools for automating more complex actions and interacting with the system.

PowerShell: a more powerful alternative to the .bat file

PowerShell is a scripting language and runtime environment developed by Microsoft designed to manage and administer Windows systems in a much more advanced way than Batch files. It’s based on .NET, and allows you to execute far more advanced commands, including :

  • Manage Windows updates and system configurations.
  • Manage Windows services and processes with Get-Service, Stop-Process, etc.
  • Manage files and folders in depth with Copy-Item, Move-Item, Remove-Item.
  • Interact with Windows registries and APIs.
  • Automate user and group management in a corporate network via Active Directory.
CriteriaBatch (.bat/.cmd)PowerShell
Command complexityVery limitedAdvanced, management of complex structures
File manipulationBasic (copy, move)Advanced (compression, extraction, content modification)
Access to Windows APIsImpossibleComplete with .NET
SecurityLow (easily modified)Better rights management and restricted execution
Interaction with the networkLimitedAdvanced features (HTTP requests, network management)

Was this article helpful to you?

Be the first person to give your opinion
× zoom plus modale
Scroll to Top