A batch file is a text file containing a list of commands that Windows runs one after another without you typing each one
A batch file is a plain-text document with a .bat or .cmd extension that holds Windows commands. When you open it, Windows reads through the file from top to bottom and executes each line as if you had typed it into Command Prompt yourself. Instead of running ten commands manually, you write them once in a batch file and run the whole sequence with a single click.
Batch files are useful for repetitive tasks on a single computer or across a network. A system administrator might use one to back up files every morning, restart computers at a set time, or copy files to shared network folders. On a local machine, you might use a batch file to organize downloads, delete temporary files, or launch multiple programs at once.
The commands inside a batch file are the same ones you would type into Command Prompt (the black window you open by pressing Windows key + R, typing "cmd", and pressing Enter). The batch file simply automates the typing.
Key Takeaways
- A batch file is a text file containing Windows commands that run in sequence when you open the file.
- You create a batch file by typing commands into Notepad, saving it with a .bat extension, and then double-clicking it to run.
- Batch files can include pauses, loops, and conditional logic so they respond differently depending on what happens during execution.
- On a network, batch files can map drives, copy files to shared folders, or run tasks on multiple computers if deployed through Group Policy or a script runner.
- Batch files run with the permissions of the user who opens them, so a standard user cannot run commands that require administrator access unless the file is set to run as administrator.
How to create and run a batch file
Open Notepad (search for it in the Windows Start menu). Type your commands, one per line. For example:
@echo off echo This is a test pause
Save the file with a .bat extension — for instance, "test.bat" — not as a .txt file. Choose "All Files" in the Save As dialog so Notepad does not add .txt automatically. Close Notepad, find the file in File Explorer, and double-click it. A Command Prompt window opens, runs your commands, and closes (unless you include a pause command, which keeps the window open until you press a key).
The first line, @echo off, tells Windows not to display each command as it runs — only the output. Without it, you see every line printed to the screen, which is usually clutter. The echo command prints text. The pause command stops execution and waits for you to press a key, so you can read the output before the window closes.
Common commands used in batch files
cd changes the current directory. dir lists files in a folder. copy duplicates files. del deletes files. mkdir creates a new folder. ipconfig shows network settings. ping tests connectivity to another computer. net use maps a network drive or connects to a shared folder.
A practical example for a network environment might look like this:
@echo off net use Z: \\server\shared /persistent:yes copy C:\Reports\*.xlsx Z:\Archive echo Backup complete pause
This maps a network drive to Z:, copies all Excel files from a local Reports folder to the network Archive folder, prints a confirmation message, and pauses so you can see the result.
Batch files with conditions and loops
Batch files can make decisions using if statements. For example, you can check whether a file exists before trying to delete it, or check the result of a command and act accordingly. You can also loop through a list of items using for statements.
An example with a condition:
@echo off if exist C:\Backup\data.zip ( echo Backup file found ) else ( echo Backup file not found )
An example with a loop that processes multiple files:
@echo off for %%f in (C:\Downloads\*.txt) do ( copy "%%f" C:\Archive )
This loop finds every .txt file in the Downloads folder and copies each one to the Archive folder. The %%f is a variable that holds the name of each file as the loop runs.
Batch files on a network
In a networked environment, batch files can be deployed to multiple computers through Group Policy (a Windows feature that pushes settings and scripts to computers on a domain) or by placing them on a shared network folder and having users run them. A system administrator might create a batch file that every employee runs at startup to map network drives, update software, or check for security patches.
A batch file can also run commands on remote computers using psexec (a tool from Microsoft) or wmic (Windows Management Instrumentation Command-line). For example, a network administrator could use a batch file to restart all computers in a department at a scheduled time, or to gather information like disk space or installed software from multiple machines.
Batch files run with the permissions of the user who opens them. If a task requires administrator access — such as installing software or modifying system settings — the batch file must be run as administrator. You can set this by right-clicking the .bat file, choosing Properties, clicking Advanced, and checking "Run as administrator".
Limitations of batch files
Batch files are simple and work on any Windows computer without additional software, but they have limits. They cannot interact with graphical windows or click buttons in programs. They run slowly compared to compiled programs. They are difficult to debug if something goes wrong — you see error messages but often have to add extra echo statements to figure out where the problem is.
For complex automation tasks, system administrators often use PowerShell (a more powerful scripting language built into modern Windows) or third-party tools. However, for straightforward tasks like copying files, mapping drives, or running a sequence of command-line programs, a batch file is quick to write and reliable.
Frequently Asked Questions
Can I run a batch file on a Mac or Linux computer?
No. Batch files are Windows-specific because they use Windows commands and Command Prompt syntax. On Mac or Linux, you would write a shell script (.sh file) instead, which serves the same purpose but uses different commands.
What is the difference between .bat and .cmd files?
.bat files are the older format and work on all versions of Windows. .cmd files are newer and have a few extra features, but both run the same commands. For most purposes, .bat is sufficient and more widely compatible.
Can a batch file spread a virus?
A batch file itself is just text and cannot carry a virus, but a malicious batch file could download or run harmful software if someone tricks you into opening it. Never run a batch file from an untrusted source. Windows may also warn you before running a .bat file downloaded from the internet.
How do I stop a batch file that is running?
Press Ctrl+C in the Command Prompt window. This interrupts the current command and stops the batch file. If the window is frozen, close it by clicking the X button, though this may leave some tasks incomplete.
Can I schedule a batch file to run automatically?
Yes, using Windows Task Scheduler. Open Task Scheduler, create a new task, point it to your batch file, and set a trigger (such as a specific time each day or when the computer starts). This is how system administrators automate routine maintenance without user intervention.