Running a batch file means executing a .bat file from the command line or by double-clicking it in File Explorer

A batch file is a text file containing a list of commands that Windows runs one after another. The file ends in .bat or .cmd. You can run it three ways: double-click it in File Explorer, right-click and select "Run as administrator", or type its name into Command Prompt or PowerShell. The simplest method for most people is double-clicking, but using the command line gives you more control and lets you see what the file is doing as it runs.

Batch files are useful for automating repetitive tasks — backing up folders, renaming files in bulk, or running the same program every morning. They run only on Windows. If you create a batch file yourself, you need to know the exact commands and their correct spelling, or the file will fail silently or show an error.

Key Takeaways

  • Double-clicking a .bat file in File Explorer is the fastest way to run it, but the window closes when ready when it finishes.
  • Running a batch file from Command Prompt lets you see the output and errors, which helps you troubleshoot if something goes wrong.
  • Right-click and select "Run as administrator" if the batch file needs permission to change system settings or access protected folders.
  • Batch files run the commands inside them in order from top to bottom, and they stop if a command fails unless you tell them to keep going.
  • You can create a batch file by opening Notepad, typing commands, and saving the file with a .bat extension instead of .txt.

Double-clicking a batch file in File Explorer

The easiest way to run a batch file is to find it in File Explorer and double-click it. Windows will open a command window, run all the commands inside the file, and then close the window. This works for batch files that do not need administrator rights and that you have already tested.

The problem with double-clicking is that the window closes as soon as the batch file finishes. If an error occurs, you may not see the message in time to read it. For this reason, many people prefer to run batch files from Command Prompt instead, where the window stays open and you can read the output.

Running a batch file from Command Prompt

Open Command Prompt by pressing Windows key + R, typing cmd, and pressing Enter. Navigate to the folder where your batch file is stored by typing cd followed by the folder path. For example, if your batch file is on your Desktop, type cd Desktop and press Enter.

Once you are in the correct folder, type the name of the batch file and press Enter. If the file is named backup.bat, type backup.bat and press Enter. The commands inside the file will run one by one. The command window will stay open so you can see any messages or errors. When the batch file finishes, you can close the window or run another command.

If the batch file is in a different drive — for example, on an external hard drive — type the drive letter first. Type D: and press Enter to switch to the D drive, then navigate to the folder with your batch file.

Running a batch file with administrator rights

Some batch files need administrator rights to work. These are files that change system settings, install software, or access folders that Windows protects. If you run a batch file without the right permissions, it will fail with an "Access Denied" error.

To run a batch file as administrator, right-click it in File Explorer and select Run as administrator. Windows will ask for permission. Click Yes. The batch file will then run with full permissions. Alternatively, open Command Prompt as administrator (right-click Command Prompt and select Run as administrator), navigate to your batch file, and run it normally.

Creating and saving a batch file

To create a batch 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 batch file might contain:

@echo off echo This is a test pause

The first line, @echo off, tells Windows not to display each command as it runs. The second line, echo This is a test, prints text to the screen. The third line, pause, keeps the window open so you can see the output.

When you are done typing, click File and then Save As. In the "Save as type" dropdown, select All Files. Type a name for your file followed by .bat — for example, test.bat. If you do not change the file type to "All Files", Notepad will save it as a .txt file instead, and Windows will not run it as a batch file. Click Save.

Understanding batch file errors and how to fix them

If a batch file does not run or stops partway through, the problem is usually a typo in a command or a file that the batch file cannot find. When you run the batch file from Command Prompt, read the error message carefully — it will tell you which line failed and why.

Common errors include: a command that does not exist (Windows will say "is not recognized as an internal or external command"), a file path that is wrong (Windows will say "The system cannot find the file specified"), or a folder you do not have permission to access (Windows will say "Access Denied"). To fix these, check the spelling of the command, verify that the file or folder exists at the path you typed, or run the batch file as administrator.

If you want the batch file to keep running even if one command fails, add setlocal enabledelayedexpansion at the top and use || between commands. For example, command1 || command2 means "run command2 even if command1 fails". Without this, the batch file stops at the first error.

Using batch files for common tasks

Batch files are most useful for tasks you do the same way every time. A common example is backing up a folder. You can create a batch file that copies a folder to an external drive with a single double-click instead of dragging and dropping files manually each time.

Another common use is renaming or moving files in bulk. If you have 50 photos named photo1.jpg, photo2.jpg, and so on, and you want to rename them all to vacation1.jpg, vacation2.jpg, you can write a batch file to do this in seconds instead of renaming them one by one. You can also use batch files to run a program on a schedule by creating a Windows Task Scheduler task that runs your batch file at a specific time each day.

Frequently Asked Questions

What is the difference between a .bat file and a .cmd file?

Both .bat and .cmd files contain the same commands and run the same way on modern Windows. The .bat extension is older and comes from DOS. The .cmd extension was introduced with Windows NT. For practical purposes, they work identically, and you can use either one. Most people use .bat out of habit.

Can I run a batch file from PowerShell instead of Command Prompt?

Yes. Open PowerShell, navigate to the folder with your batch file using cd, and type the filename. PowerShell will run it the same way Command Prompt does. Some people prefer PowerShell because it has more advanced features, but for straightforward batch files, Command Prompt and PowerShell behave the same.

Why does my batch file close when ready when I double-click it?

The batch file ran and finished before you could read the output. Add the line pause at the end of your batch file. This tells Windows to wait for you to press a key before closing the window. Open the batch file in Notepad, add pause as the last line, save it, and try again.

Can I schedule a batch file to run automatically at a certain time?

Yes, using Windows Task Scheduler. Open Task Scheduler (search for "Task Scheduler" in the Start menu), click Create Basic Task, give it a name, set the trigger (the time you want it to run), and set the action to "Start a program". Browse to your batch file and click OK. The batch file will run automatically at the time you specified.

What should I do if I get an "Access Denied" error?

The batch file needs administrator rights. Right-click the batch file and select Run as administrator, or open Command Prompt as administrator, navigate to the batch file, and run it from there. If you created the batch file yourself, make sure the commands inside it are correct — some commands require administrator rights even when run from an administrator command prompt.