The simplest way to run a batch file

To run a batch file from Command Prompt, type the filename (with the .bat extension) and press Enter. If the file is in your current directory, that is all you need. If it is somewhere else on your computer, you need to type the full path to the file, or navigate to its folder first using the cd command.

For example, if a file called backup.bat is on your Desktop, you would type one of these:

Desktop\backup.bat (if you are already in your user folder)

Or navigate there first: cd Desktop, then type backup.bat

Command Prompt will then run the commands inside the batch file in order, line by line, and show you the output as it goes.

Key Takeaways

  • Type the batch filename with its full path, or navigate to its folder using cd first, then type just the filename.
  • Windows searches for batch files in your current directory and in folders listed in your system PATH, so you may not need the full path if the file is in a common location.
  • Use pause at the end of your batch file to keep the Command Prompt window open after the commands finish running.
  • If you get a "file not found" error, check that you spelled the filename correctly and that the path is accurate.

Understanding paths and the current directory

Command Prompt always has a current directory — the folder it is currently looking at. When you type a filename without a path, Command Prompt searches for it in the current directory first. You can see your current directory in the prompt itself; it usually looks like C:\Users\YourName>.

If your batch file is not in the current directory, you have two choices: give the full path, or change directories. To change directories, use the cd command followed by the path. For example, cd C:\Users\YourName\Documents moves you to your Documents folder. Then type the filename alone.

You can also use relative paths, which describe where a folder is compared to where you are now. For example, if you are in C:\Users\YourName and your batch file is in C:\Users\YourName\Documents\Scripts, you can type Documents\Scripts\myfile.bat instead of the full path.

Running batch files with administrator rights

Some batch files need administrator permissions to run — for example, if they install software or change system settings. If you run one without the right permissions, it will fail silently or show an error partway through.

To run a batch file as administrator from Command Prompt, type runas /user:Administrator "C:\path\to\file.bat" and press Enter. Windows will ask for the administrator password. Replace the path with the actual location of your file.

Alternatively, you can open Command Prompt itself as administrator first (right-click Command Prompt and choose "Run as administrator"), then run the batch file normally. This is often simpler if you have multiple files to run.

Keeping the window open to see the results

By default, Command Prompt closes the window immediately after a batch file finishes running. If the file runs quickly, you may not see the output at all. To keep the window open, add the word pause on a new line at the very end of your batch file. This tells Command Prompt to wait for you to press a key before closing.

If you cannot edit the batch file itself, you can also run it with the /k switch, which keeps the window open. Type cmd /k C:\path\to\file.bat instead of running the file directly.

Troubleshooting "file not found" errors

If Command Prompt says the file is not found, the most common cause is a typo in the filename or path. Check that you spelled everything correctly, including the .bat extension. Windows is not case-sensitive for filenames, so Backup.bat and backup.bat are the same file.

Another common issue is that the file is in a different location than you think. Use the dir command to list the files in your current directory and confirm the batch file is there. If it is not, use cd to navigate to the correct folder, or type the full path to the file.

If the file is on a network drive or external drive, make sure that drive is connected and accessible. Network paths usually start with two backslashes, like \\servername\foldername\file.bat.

Running batch files from other programs

You can also run a batch file from other applications without opening Command Prompt manually. In many programs, you can create a shortcut or task that calls the batch file. The syntax is usually cmd /c C:\path\to\file.bat, where /c tells Command Prompt to run the file and then close the window.

Windows Task Scheduler is a common way to run batch files on a schedule. You create a new task, set the action to run cmd.exe, and pass the batch file path as an argument. This is useful for backups, cleanup routines, or other jobs you want to run automatically at specific times.

Frequently Asked Questions

What is the difference between running a batch file and double-clicking it?

Double-clicking a batch file runs it directly, while running it from Command Prompt lets you see the output and control what happens next. If the file closes too quickly, you will not see any errors. Running from Command Prompt gives you more visibility and control.

Can I run a batch file from a different drive?

Yes. If you are on drive C: and the file is on drive D:, type D: to switch to that drive, then navigate to the folder and run the file. Or type the full path including the drive letter, like D:\foldername\file.bat.

What does the /c switch do?

The /c switch tells Command Prompt to run a command and then close the window. The /k switch does the same thing but keeps the window open. Use /c when you want the file to run silently in the background, and /k when you want to see the output.

How do I pass arguments to a batch file from Command Prompt?

Type the batch filename followed by the arguments, separated by spaces. For example, backup.bat D: weekly passes two arguments to the file. Inside the batch file, you access them as %1 (first argument) and %2 (second argument).

Why does my batch file say "access denied"?

This usually means the file needs administrator permissions. Try running Command Prompt as administrator first, then run the batch file. If that does not work, check that your user account has permission to read and execute the file.