A batch file is a plain text file that contains a list of commands Windows runs one after another
A batch file (or .bat file) is a simple text document that holds Windows commands. When you open it, Windows reads through the file from top to bottom and executes each command in order, without stopping to ask you what to do next. The file extension is .bat or .cmd.
You write batch files in Notepad or any text editor. Each line is a single command — the same commands you would type into Command Prompt yourself. The difference is that a batch file runs them all at once, automatically, which saves time when you need to repeat the same steps over and over.
Batch files are one of the oldest automation tools in Windows. They are not as powerful as modern scripting languages like PowerShell, but they are simpler to learn and work on every version of Windows without extra software.
Key Takeaways
- A batch file is a text file containing Windows commands that run automatically when you open it, one command per line.
- You create batch files in Notepad by typing commands, saving the file with a .bat extension, and then double-clicking to run it.
- Batch files are useful for repetitive tasks like copying files, renaming groups of documents, or starting multiple programs at once.
- Batch files run in Command Prompt and cannot do things that require a graphical interface, such as clicking buttons in a window.
How to create and run a batch file
Open Notepad (or any plain text editor). Type the commands you want to run, one per line. For example, a simple batch file might look like this:
echo Hello, this is a batch file dir C:\Users pause
Save the file with a .bat extension — for example, myscript.bat. Do not save it as a .txt file. When you double-click the .bat file, Windows opens Command Prompt, runs each command in order, and then closes (unless you add a pause command, which keeps the window open so you can see the results).
If you want to run the batch file from Command Prompt instead of double-clicking it, type the filename (including the path if it is not in your current folder) and press Enter.
Common commands used in batch files
Batch files use the same commands available in Command Prompt. Here are the ones you will see most often:
- echo — prints text to the screen
- cd — changes the current folder
- dir — lists files in a folder
- copy — copies a file from one location to another
- del — deletes a file
- ren — renames a file
- pause — stops the batch file and waits for you to press a key
- start — opens a program or file
- if — runs a command only if a condition is true
A batch file can also use variables (temporary storage for information), loops (repeating the same command multiple times), and conditional logic (if this, then do that). These features let you write batch files that make decisions and adapt to what they find.
What batch files can and cannot do
Batch files are good at file management, running programs, and automating repetitive command-line tasks. You can use them to back up folders, rename dozens of files at once, delete old temporary files, or start multiple programs in a specific order.
Batch files cannot interact with graphical windows — they cannot click buttons, fill in forms, or read what is displayed on screen. If a task requires clicking inside a program window, a batch file is not the right tool. For those jobs, you would use a more advanced scripting language like PowerShell or a dedicated automation tool.
Batch files also run with the same permissions as the user who opens them. If you need to do something that requires administrator rights, you must run the batch file as administrator.
Batch files versus PowerShell scripts
PowerShell is a newer scripting language built into modern Windows systems. It is more powerful than batch files and can do things batch files cannot, such as working with objects, connecting to remote computers, and managing system settings in detail.
Batch files are simpler and have a gentler learning curve. If you only need to run a few basic commands in sequence, a batch file is faster to write and easier to understand. If you need advanced features or plan to write complex automation, PowerShell is the better choice.
Both tools are free and built into Windows. You do not need to install anything to use either one.
Real-world examples of batch file uses
A common use is backing up a folder. You could write a batch file that copies your Documents folder to an external drive every time you run it, without typing the copy command manually each time.
Another example is cleaning up temporary files. Windows stores temporary files in several locations. A batch file can delete all of them at once by running multiple delete commands in sequence.
You could also use a batch file to start your work environment — opening your email client, web browser, text editor, and project management tool all at once, instead of clicking each one individually.
System administrators often use batch files to deploy software across many computers, configure network settings, or run maintenance tasks on a schedule.
Security and safety with batch files
A batch file is just text, so you can open it in Notepad and read exactly what it will do before you run it. This makes it safer than downloading an executable file from the internet, where you cannot see what the code actually does.
However, a batch file can still delete files, change settings, or install software. Only run batch files from sources you trust. If someone sends you a .bat file and you are not sure what it does, open it in Notepad first and read through the commands.
Be especially careful with batch files that use the del command (delete) or format command (erase a drive). These commands cannot be undone, so double-check the file path before you run them.
Frequently Asked Questions
Can I run a batch file on a Mac or Linux computer?
No. Batch files are specific to Windows. On Mac and Linux, you would use shell scripts (.sh files) instead, which work similarly but use different commands. The concepts are the same, but the syntax and available commands are different.
What happens if a command in my batch file fails?
By default, the batch file continues to the next command even if one fails. If you want the batch file to stop when an error occurs, you can add error-handling code using the errorlevel variable or the if errorlevel command to check whether the previous command succeeded.
Can I schedule a batch file to run automatically at a certain time?
Yes. Windows Task Scheduler lets you set up a batch file to run on a schedule — daily, weekly, or when a specific event happens. You do not need to open it manually. Open Task Scheduler, create a new task, point it to your .bat file, and set the schedule you want.
Is a .cmd file the same as a .bat file?
They are nearly identical. Both run the same commands in Command Prompt. The .cmd extension was introduced in Windows NT and is slightly more modern, but .bat files still work on all Windows versions. For most purposes, you can use either extension.
How do I pass information into a batch file when I run it?
You can use command-line arguments. When you run a batch file, you can add values after the filename, and the batch file can read them using %1, %2, %3, and so on. For example, running myscript.bat C:\folder would let the batch file read C:\folder as %1 and use it in commands.