Open In App

Writing a Windows batch script

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Article Tags :

Similar Reads