A bat file is a plain-text file that tells Windows to run a series of commands in order, one after another

The name comes from "batch" — the file bundles multiple commands into a single task that runs without you typing each one. When you double-click a bat file (also called a batch file), Windows opens Command Prompt in the background and executes every line you wrote inside it. A bat file is always a text file with a .bat extension at the end of its name, like backup.bat or cleanup.bat.

Bat files are useful because they let you automate repetitive work. Instead of typing the same five commands every morning to clear temporary files, check disk space, and restart a service, you write those commands once in a bat file and run the whole sequence with a single click. Windows has used batch files since the 1980s, and they still work the same way today.

Key Takeaways

  • A bat file is a text file containing Windows commands that run one after another when you open it.
  • You create a bat file by typing commands into Notepad, saving it with a .bat extension, and then double-clicking it to run.
  • Bat files can perform tasks like copying files, deleting old data, restarting services, or running other programs in a set order.
  • Windows runs bat files through Command Prompt, so you need to know the actual command names and syntax that Command Prompt understands.

How to create and run a bat file

Open Notepad (search for "Notepad" in the Windows Start menu). Type the commands you want to run, one per line. For example, a straightforward bat file might look like this:

cd C:\Users\YourName\Documents dir pause

The first line changes to your Documents folder. The second line lists everything in that folder. The third line pauses so you can see the results before the window closes. Save the file with a .bat extension — click File, then Save As, type a name like mycommands.bat, and make sure you save it as "All Files" type, not as a .txt file. Then double-click the bat file to run it. Windows will open a Command Prompt window and execute each line in order.

If a command fails or you want to stop the batch, you can close the Command Prompt window at any time. The pause command at the end is useful because it keeps the window open so you can read any error messages or results before it closes automatically.

Common commands you can put in a bat file

The commands available in a bat file are the same ones you would type into Command Prompt directly. Some of the most useful ones for maintenance are del (delete files), copy (copy files), xcopy (copy entire folders), dir (list files), cd (change folder), and ipconfig (show network settings). You can also run other programs — for example, typing notepad.exe will open Notepad, or calc.exe will open Calculator.

A practical example: if you want to delete all temporary files from your Downloads folder every week, you could create a bat file with these lines:

cd C:\Users\YourName\Downloads del *.tmp del *.temp echo Cleanup complete pause

The echo command prints text to the screen, so you see a message when the cleanup finishes. The asterisk (*) is a wildcard that means "all files" — so del *.tmp deletes every file ending in .tmp.

Why bat files are limited compared to other scripting languages

Bat files work, but they are straightforward and have real limits. They cannot easily check if a file exists before deleting it, they struggle with complex logic, and they run slowly compared to other automation tools. If you need to do something more sophisticated — like checking system health, sending emails, or managing user accounts — you will eventually outgrow batch files.

Windows also offers PowerShell, which is a more powerful scripting language built into modern Windows versions. PowerShell can do everything a bat file can do, plus much more, and it is the tool Microsoft now recommends for automation. However, bat files are still useful for straightforward, straightforward tasks, and they require no special setup or permissions to run.

Running a bat file automatically on a schedule

You can set Windows to run a bat file at a specific time without you having to click it. The easiest way is to use Task Scheduler, which is built into Windows. Search for "Task Scheduler" in the Start menu, open it, and click "Create Basic Task" on the right side. Give the task a name, set the trigger (when it should run — daily, weekly, at startup, etc.), and then set the action to "Start a program" and point it to your bat file.

Task Scheduler will run your bat file at the time you choose, even if you are not at your computer. This is how many people automate backups, cleanup routines, and system checks. If you want the bat file to run with administrator permissions (which some commands require), you can set that in the task properties under "General" by checking "Run with highest privileges."

Security and safety with bat files

A bat file can delete files, change settings, or install software, so only run bat files you wrote yourself or trust completely. If someone sends you a bat file, do not run it unless you understand what it does. You can open any bat file in Notepad to read the commands before running it — this is one advantage of batch files being plain text.

Windows may also warn you when you try to run a bat file, especially if it came from the internet. This is a safety feature. If you created the file yourself, you can ignore the warning and run it. If you did not create it and do not recognize it, do not run it.

Frequently Asked Questions

Can I run a bat file from a USB drive on a different computer?

Yes. A bat file is just text, so it works on any Windows computer. However, if the bat file refers to specific folder paths (like C:\Users\YourName\Documents), those paths may not exist on the other computer. You can make bat files more flexible by using variables like %USERPROFILE% instead of typing out the full path, so they work on any computer.

What happens if a command in my bat file fails?

By default, the bat file keeps running the next command even if one fails. If you want it to stop when an error occurs, add setlocal enabledelayedexpansion at the top and use if errorlevel 1 exit /b 1 after commands that must succeed. For straightforward tasks, letting it continue is usually fine — you will see the error message and can fix it next time.

Is a bat file the same as a .cmd file?

A .cmd file is nearly identical to a .bat file and works the same way. The difference is historical — .cmd files were introduced in Windows NT and support a few extra features, while .bat files date back to DOS. For most purposes, they are interchangeable, and you can use either extension.

Can I edit a bat file after I create it?

Yes. Right-click the bat file, select "Open with," and choose Notepad. Make your changes, save the file, and the next time you run it, the new commands will execute. You do not need to restart your computer or do anything special — just save and run it again.