What a .bat file is and why you might make one
A .bat file is a plain text file that contains a list of commands your Windows computer runs one after another, without you typing each one. The ".bat" stands for "batch" — it batches commands together. When you double-click the file, Windows opens Command Prompt and runs every line in order.
You might make one to automate something you do repeatedly: backing up a folder, opening several programs at once, renaming files in bulk, or running a maintenance task on a schedule. Instead of typing the same commands every time, you write them once in a .bat file and run the whole sequence with a single click.
A .bat file is just text, so anyone with a text editor can read what it does. It runs with the same permissions you have — if you can delete a file by hand, your .bat file can too. That is why you should only run .bat files from sources you trust.
Key Takeaways
- A .bat file is a text file containing Windows commands that run in sequence when you open it, letting you automate repetitive tasks.
- You create a .bat file by writing commands in Notepad, saving it with a .bat extension instead of .txt, and making sure "Save as type" is set to "All Files".
- Common commands include cd to change folders, copy to duplicate files, del to delete files, and pause to keep the window open so you can see the results.
- You run a .bat file by double-clicking it or right-clicking and choosing "Run as administrator" if the task needs higher permissions.
- Only run .bat files from sources you trust, because they can do anything your user account can do on the computer.
Opening Notepad and writing your first commands
Start by opening Notepad. Press the Windows key, type "notepad", and press Enter. A blank window opens.
Type the commands you want to run. Each command goes on its own line. For example, if you want to create a new folder called "Backup" on your Desktop, you would type:
cd Desktop mkdir Backup
The first line changes to your Desktop folder. The second line creates a new folder named "Backup" inside it. If you want the Command Prompt window to stay open after the commands finish so you can see what happened, add this line at the end:
pause
The pause command makes Windows wait for you to press a key before closing the window. Without it, the window closes when ready and you might miss error messages or results.
Saving the file with the .bat extension
Go to File > Save As. A dialog box opens. At the bottom, you see "Save as type:" with a dropdown menu. Click that dropdown and select "All Files (*.*)" — this is the critical step. If you leave it on "Text Documents (*.txt)", Windows will save your file as .txt even if you type .bat in the filename.
In the filename field, type a name followed by .bat. For example: create_backup.bat. Choose where to save it — your Desktop is easiest to find later. Click Save.
Go back to your Desktop or the folder where you saved it. You should see a file with a gear icon (or a blank page icon, depending on your Windows version). That is your .bat file. If you see a text document icon instead, you saved it as .txt by mistake — go back to File > Save As, change the type to "All Files", and save again with the .bat extension.
Common commands you can use in a .bat file
Here are commands that appear in most .bat files:
| Command | What it does | Example |
|---|---|---|
| cd | Changes to a different folder | cd C:\Users\YourName\Documents |
| mkdir | Creates a new folder | mkdir NewFolder |
| copy | Copies a file to another location | copy file.txt C:\Backup\ |
| del | Deletes a file | del oldfile.txt |
| echo | Prints text to the screen | echo Backup complete |
| pause | Pauses and waits for you to press a key | pause |
| start | Opens a program or file | start notepad.exe |
If a folder path has spaces in it, wrap the path in quotes. For example: cd "C:\Program Files\MyApp". Without quotes, Windows stops at the first space and gets confused.
Running your .bat file
Double-click the .bat file. A Command Prompt window opens and runs each command in order. You see text appear as each command executes. If you included pause at the end, the window stays open until you press a key.
If you see an error like "command not recognized" or "file not found", the most common cause is a typo in the path or filename. Check that folder names and file names match exactly, including capital letters (though Windows usually does not care about capitals).
If the .bat file needs to do something that requires administrator permission — like installing software or changing system settings — right-click the .bat file and choose "Run as administrator" instead of double-clicking. Windows will ask for permission, and then the commands run with higher privileges.
Editing a .bat file after you create it
To change what a .bat file does, right-click it and choose "Edit" (or "Open with" > Notepad). Make your changes and save the file. The next time you run it, it will use the new commands.
If you accidentally double-click a .bat file and it starts running commands you did not want, close the Command Prompt window when ready. The commands that already ran will have taken effect, but you can stop any that have not started yet.
Frequently Asked Questions
Can I run a .bat file on a Mac or Linux computer?
.bat files are Windows-only. On a Mac, you would create a shell script (.sh file) instead. On Linux, you would also use a shell script. The commands are different because the operating systems work differently, but the idea is the same: a text file that runs commands in sequence.
What happens if I double-click a .bat file by accident?
The commands start running. Close the Command Prompt window as fast as you can to stop any commands that have not finished yet. Commands that already ran will have taken effect — for example, if the file deletes something, it is deleted. This is why you should only run .bat files you wrote yourself or trust completely.
Can I schedule a .bat file to run automatically at a certain time?
Yes, using Windows Task Scheduler. Press the Windows key, type "task scheduler", and open it. Click "Create Basic Task", give it a name, set a trigger (like "daily at 9 AM"), and point it to your .bat file. Windows will run it on the schedule you set, even if you are not at the computer.
Why does my .bat file keep saving as .txt?
In Notepad, the "Save as type" dropdown is probably still set to "Text Documents". Click that dropdown, choose "All Files (*.*)", and then type your filename with .bat at the end. If you have already saved it as .txt, delete that file and save a new one with the correct type selected.
Can I hide the Command Prompt window while the .bat file runs?
Yes. At the very top of your .bat file, add this line: @echo off. This hides most of the text that would normally appear. If you want to hide the window itself, you can use a VBScript wrapper, but that is more advanced. For most tasks, @echo off is enough to keep the window from being distracting.