What is a batch file?
A batch file is a script file that stores commands to be executed in a serial order. It helps automate routine tasks without requiring user input or intervention. Some common applications of batch files include loading programs, running multiple processes or performing repetitive actions in a sequence in the system.
Batch files usually have the .bat extension. However, they can also have the .cmd extension. Other names for batch files include scripts, shell scripts and command files.
What is a batch file used for?
Also known as a batch job, a batch file is a text file created in Notepad or some other text editor for use with the Windows operating system (OS). The same editor can also be used to edit the batch file. The file bundles or packages a set of commands into a single file in serial order. It eliminates the need to retype commands manually. Without it, these commands would have to be presented one at a time to the system with input from a keyboard, so a batch file saves time for users and helps them avoid mistakes.
The chief use of a batch file is to simplify complex processes and to automate common tasks. It is ideal for users who regularly open a particular website, create or rename files in bulk, or check Internet Protocol (IP) addresses using the ipconfig command. It automates these routine tasks, saving the user time and effort.
The many uses of batch files include the following:
- Copying, renaming and deleting files.
- Creating directories.
- Starting applications.
- Shutting down processes.
- Modifying system settings.
- Performing system maintenance.
How to create a batch file
Usually, a batch file is created as a plain text file with a .bat extension for command sequences that need to be performed repetitively. The file creation process is simple and consists of only a few steps:
- Open a text editor, such as Notepad.
- Start with the @echo off command.
- Type these commands into the editor one line at a time:
title [title of the batch script]
echo [first line]
pause
- At this point, the file will look like this:
@echo off
title My first batch file
echo Congratulations and welcome to your first batch file!
pause
Save the file with a .bat, .btm or .cmd extension.
When naming a batch file, the name should not include any spaces because they can cause issues when the file is called from other scripts. It is also important not to use common batch file names, especially if those files are already available in the system, such as ping.cmd.
In addition to commands, batch files can also include comments that have no effect on code execution and are not executable. Their main purpose is to provide information about the file. For example, a user might add a comment explaining the file's purpose, another about its creation date, a third to explain why a particular command has been temporarily disabled and so on. They can do this with the help of the remarks command, rem.
How to run a batch file
There are two ways to run a batch file:
- Double-click on the file in the text editor application. When this is done, the application will display the code in the file. Also, Windows will execute the commands within it one at a time.
- Open a command prompt window, type the full path for the file location, and press Enter. With this method, another option is to open the folder containing the file, type the batch file's name, and then press Enter.
A command-line interpreter takes the file as an input and executes the commands in the given order. The interpreters of some OSes also support additional commands for specific purposes. For example, the interpreter of IBM's OS/2 OS supports the extproc command, which passes the batch file as a data file. This command instructs cmd.exe to automatically call a REXX program from the data file, which can then be used for automating tasks and performing data manipulations.
It's important to note that only knowledgeable users should handle batch files. Inexperienced users might make mistakes when opening or running these files, which could result in important system files or personal files getting deleted or damaged.
Users should also be careful about running batch files downloaded from a website or received through email or chat. Since each batch file has an executable extension, opening it without knowing its integrity can end up harming the system.
That said, the risk can be mitigated by converting the file to a format like EXE. Numerous tools are available to help with such conversions, including Windows' built-in IExpress tool and free tools like NSSM and Microsoft PowerShell Scriptomatic.
A batch file can also be converted to TXT, since it is a text file, albeit with a different file extension. However, such conversions might prevent it from executing its commands, so it's important to do this only when required.
What is an example of a batch file?
A batch file can contain many different commands. For example, one command might be to empty the clipboard automatically. The code for this would be as follows:
cmd /c "echo off | clip"
Similarly, the command to ping a router with a specific IP address, XXX.YYY.Z.Z, and confirm if the system is able to reach it would be as follows:
ping XXX.YYY.Z.Z
pause
Batch files in Windows and other OSes
The Microsoft Windows OS typically uses the .bat file extension for batch files. However, other types of batch file extensions are also available in Windows:
- .ini, used for initialization files that set default variables for the system and its programs.
- .sys, used for system files.
- .cfg, used for configuration files.
- .com, used for executable command files that contain Disk Operating System (DOS) commands.
Batch files in Windows are also created using the .cmd or .btm file extensions.
The .bat extension also applies to batch files in DOS. One of the best-known DOS batch files is autoexec.bat, which initializes DOS at system startup. In Unix operating systems, a batch file is called a shell script.
To run typical commands -- such as to modify system settings, start apps or launch a website -- batch files can be run using the command prompt. Tools like PowerShell for Windows and Bash for Linux environments can be used to create advanced batch file scripts.
Basic commands in batch files
To create and execute a batch file, users should understand the basic commands, including the following:
- Echo. Echo lets users display messages and is usually on by default, but it can be turned off. When echo is on, users can view the characters entered on screen. When echo is off, input cannot be seen.
- Title. This command changes the title text appearing at the top of the command window.
- Rem. Rem statements are typically used to explain or comment on the code. They do not form a part of the code being executed.
- Cls. The cls command clears the prompt screen.
- Pause. The pause command stops the execution of the batch file. After running this command, the window stays open, giving users the option of proceeding further or pausing between each task. If this command is not used, the window will close automatically when the script finishes executing.
- Start [website URL]. This command uses the default browser to open a website indicated by the URL in the command.
- Ipconfig. The ipconfig command displays network information, e.g., media access control addresses, IP addresses and subnets.
- Ping. The ping command tests an IP address by sending out data packets and then gauging their location and latency.
- Dir. This command will retrieve all the directories, subdirectories and files from the current working directory.
- Ver. The ver command can be used to find out the version of Windows installed on the system.

Variables and parameters in batch files
Variables can be used in batch files to store specific values of data and enable users to perform specific instructions on that data, even if they don't specify the data type. The set command is used to create variables in batch files.
Take the following example:
set my_variable=Hello TechTarget!
This variable value can be accessed with the syntax %variable% and printed on screen with the echo command:
echo %my_variable%
Batch files also accept parameters and loops. To access the parameters, users can use special variables called command-line arguments. To create a loop, the for command is used. This enables users to execute a specific block of code multiple times.
Windows admins have the option to use the Linux shell Bash, but how does the command-line tool stack against PowerShell for Windows deployments? Explore the key differences between PowerShell vs. Bash.