Writing a Windows batch script
Last Updated :
11 Jul, 2025
A batch file in Windows is a plain text file that contains a series of commands to be executed by the command-line interpreter (cmd.exe
). These commands are run in the order they appear, making batch files useful for automating repetitive tasks.
A batch file uses the .bat
file extension and can be created using Notepad or any other text editor.
:: When echo is turned off, the command prompt doesn't appear in the Command Prompt window.
ECHO OFF
:: The following command writes GeeksforGeeks to the console.
ECHO GeeksforGeeks
:: The following command suspends the processing of a batch program and displays the prompt.
PAUSE
After saving it with a .bat extension. Double click it to run the file. It prints shows:
In the above script, ECHO off cleans up the console by hiding the commands from being printed at the prompt, ECHO prints the text “GeeksforGeeks” to the screen, and then waits for the user to press a key so the program can be ceased.
Basic Batch File Commands and Their Uses
Command | Usage Description |
---|
ECHO OFF | Hides command output in the Command Prompt, used to make scripts cleaner. |
---|
ECHO | Displays a message or turns command echoing ON/OFF. If ON, CMD shows each command as it's executed. |
---|
CLS | Clears the Command Prompt screen. |
---|
TITLE | Changes the title text displayed at the top of the Command Prompt window. |
---|
EXIT | Closes the Command Prompt or exits the script. |
---|
PAUSE | Pauses the script's execution and displays “Press any key to continue...”. |
---|
:: or REM | Adds comments in a batch file to explain or document parts of the script. |
---|
COPY | Copies one or more files from one location to another |
---|
Types of Batch Files in Windows
File Type | Extension | Description |
---|
Initialization File | *.ini | Used to set default variables in the system and programs during startup or execution. |
---|
Configuration File | *.cfg | Stores configuration settings for applications and scripts. |
---|
System File | *.sys | System-level files, often compiled in machine code; some can be edited manually. |
---|
Command File (DOS) | *.com | Executable command files for DOS commands. Earlier systems had individual .com files; now included in COMMAND.COM . |
---|
Command Script (Windows NT) | *.cmd | Batch files used in NT-based Windows operating systems (like Windows 2000, XP, etc.). Similar to .bat but used for NT command interpreter. |
---|
Lets take another example, Suppose we need to list down all the files/directory names inside a particular directory and save it to a text file, so batch script for it will be,
@ECHO OFF
// A comment line can be added to the batch file with the REM command.
REM This is a comment line.
REM Listing all the files in the directory Program files
DIR"C:\Program Files" > C:\geeks_list.txt
ECHO "Done!"
Now when we run this batch script, it will create a file name geeks_list.txt in your C:\ directory, displaying all the files/folder names in C:\Program Files. Another useful batch script that can be written to diagnose your network and check performance of it:
// This batch file checks for network connection problems.
ECHO OFF
// View network connection details
IPCONFIG /all
// Check if geeksforgeeks.com is reachable
PING geeksforgeeks.com
// Run a traceroute to check the route to geeksforgeeks.com
TRACERT geeksforgeeks.com
PAUSE
This script displays:
This script gives information about the current network and some network packet information. ipconfig /all helps to view the network information and ping & tracert to get each packet info. Learn about ping and traceroute here.
Similar Reads
How to Create a Batch File in Windows? A batch file is a straightforward text document in any version of Windows with a set of instructions meant to be run by Windows' command-line interpreter (CMD) by maintaining all the functions. Batch files are a great tool for streamlining internally configured processes, automating tedious jobs, an
5 min read
Command Prompt Hacks for Windows The Command Prompt in Windows is a powerful tool that allows users to perform a wide range of tasks, from basic file management to advanced system commands. However, many users are unaware of the numerous Command Prompt hacks for Windows that can enhance productivity and simplify complex tasks. In t
6 min read
How to Create an Infinite Loop in Windows Batch File? Windows batch files are a powerful tool for automating repetitive tasks, managing system operations, and streamlining workflows. But what happens when you want to create a process that runs indefinitely?In this blog post, we'll walk you through the steps to create an infinite loop in a Windows batch
4 min read
How to Create a Simple Shutdown Timer by using Notepad? In this article you will see how you can create your own shutdown timer using a notepad. Sometimes you need to schedule the shutdown time of your PC, so in such situations, this timer is very helpful. Let's see how you can create it. Shutdown Timer By Using Notepad Follow the below steps to implemen
1 min read
How to Delete a File or Folder Using CMD When working with files on Windows, File Explorer usually makes creating and deleting files simple. But what if File Explorer freezes or stops responding? In such cases, the Command Prompt (CMD) offers a reliable alternative. CMD allows you to create and delete files or folders using simple commands
4 min read
How to Create Any File Using CMD: Learn 5 Methods Creating a file using Windows GUI is quite simple but did you know you can do the same using the Command Prompt (CMD)? This guide will walk you through multiple methods to create files directly from CMD, giving you more control and efficiency in managing your system.How to Create a File in CMDMethod
4 min read