How to Run a Windows Batch File from the Command Line (CMD)

Download Article
Easily run .BAT files in Windows and troubleshoot common problems
Download Article

If you need to run a batch file at the Windows Command Prompt, you'll just need to know the location of the batch file. To run the file at the command line, simply type the full path to the batch file and press Enter. Or, if you don't want to type the full path to the file, you can use the cd command to enter the folder containing the .BAT file, type the batch file's name, and then press Enter. This How.com.vn guide will teach you 2 simple ways to run a batch file from the Command Prompt in Windows, help you fix common errors, and show you a few other cool ways to run batch files on your PC.

Things You Should Know

  • Use the "cd" command to enter the folder that contains your batch file.
  • To run the batch file, type its name and press "Enter."
  • You can also type the full path to the batch file from any folder to run batch files from anywhere.
Section 1 of 4:

Running from the Batch File's Folder

Download Article
  1. How.com.vn English: Step 1 Open the Command Prompt.
    If your batch file performs administrative tasks, open Command Prompt as an administrator. If the batch file only requires access to files in your user account, you don't need to start it as an administrator.
    • Press the Windows key on your keyboard.
    • Type cmd.
    • If your batch file doesn't need administrator rights, click Command Prompt.
    • If your batch file does require administrator rights:
      • Right-click Command Prompt.
      • Select Run as administrator.
      • Click Yes.
  2. How.com.vn English: Step 2 Type cd followed by the full path to your batch file's folder.
    The cd command stands for "change directories." For example:
    • If the file is at D:\Music\MP3s\Unsorted, you'd type cd D:\Music\MP3s\Unsorted.
    • If the batch file is on your desktop, you'd type cd \Users\YourLoginName\Desktop.
      • If you use OneDrive to automatically back up your personal files, you'd use cd \Users\YourLoginName\OneDrive\Desktop instead.
      • If you don’t know your login name, type cd \Users and press Enter. Then, type dir and press Enter to see a list of users.
    Advertisement
  3. How.com.vn English: Step 3 Press ↵ Enter.
    This will move you into the folder containing your batch file.
    • Type dir and press Enter to see a list of all files in the current folder. You should see your batch file (ending with .bat) here.
  4. How.com.vn English: Step 4 Type the name of the batch file and press ↵ Enter.
    For example, if your batch file is called program.bat, type program.bat and press Enter. This runs the batch file.
  5. Advertisement
Section 2 of 4:

Running from Anywhere

Download Article
  1. How.com.vn English: Step 1 Get the full path to your batch file.
    If you want to run a batch file without using cd to enter its folder first, you'll just need to know the full path to the file. For example, C:\Users\How.com.vn\Scripts\mybatchfile.bat. Here's an easy way to find the full path to your batch file:
    • Navigate to your batch file. If you're not sure where you saved it, you can search for it.
    • If you're using Windows 11, right-click the file and select Copy as path (Windows 11).
    • On Windows 10 and earlier, right-click the file and select Properties. Highlight the path next to "Location" and press Ctrl + C to copy it to your clipboard.
  2. How.com.vn English: Step 2 Open the Command Prompt.
    If your batch file performs administrative tasks, open Command Prompt as an administrator. If it only requires access to files in your user account, you don't need to start it as an administrator.
    • Press the Windows key on your keyboard.
    • Type cmd.
    • If your batch file doesn't need administrator rights, click Command Prompt.
    • If your batch file does require administrator rights:
      • Right-click Command Prompt.
      • Select Run as administrator.
      • Click Yes.
  3. How.com.vn English: Step 3 Type or paste the full path to your batch file.
    To paste the path you copied earlier, press Ctrl + V.
    • Alternatively, you can type the full path to your file manually (e.g., C:\Users\How.com.vn\Scripts\mybatchfile.bat).
    • Because the .BAT extension is executable, you don't need to type any commands before pasting or entering the path to the file.
  4. How.com.vn English: Step 4 Press ↵ Enter to run the batch file.
    The batch file will now run. Any commands in the batch file will execute as specified in the script.
  5. Advertisement
Section 3 of 4:

Troubleshooting

Download Article
  1. How.com.vn English: Step 1 Running the batch file at the prompt just takes me to the next line.
    Depending on the commands in your batch file, you may not see confirmation or output when you run a .BAT file from Command Prompt. You'll only see output if there's a command in the batch file that's supposed to display output.
    • For example, if the batch file is just working with files on your PC, such as moving, renaming, and/or deleting, you won't see the commands as they run unless @echo on is at the top of the .BAT file.[1]
    • You can also add echo "your text here" to your batch file to display a custom message when run.
  2. Step 2 I see "The syntax of the command is incorrect" when I run a batch file.
    This means that there's an error with one of the commands in the batch file. The error could be due to missing or incorrect symbols, leaving out a flag required by a command, or even missing quotation marks around paths. Open the batch file in an editor like Notepad and inspect the code for errors.
  3. How.com.vn English: Step 3 I'm getting the error "x is not recognized as an internal or external command."
    This error also indicates an error in the batch file. But in this case, the error occurs because a command in the batch file is not recognized by Command Prompt. Fortunately, the error tells you exactly which command is failing, so it should be simple to fix.
    • For example, if a command in the .BAT file is misspelled or incorrect (e.g., "ifconfig" instead of "ipconfig"), or trying to start a program using its name instead of its full path.
  4. How.com.vn English: Step 4 When I double-click a batch file, the window just closes.
    If double-clicking your .BAT file quickly opens and closes a Command Prompt window, it just means the batch file doesn't have a "pause" at the end. If you want to keep the window open after the commands have run, just add "pause" to the end of the batch file.
    • Right-click the batch file.
    • Select Open with > Notepad (Windows 10) or Show more options > Edit (Windows 11).
    • Add "pause" to the last line of the batch file.
    • Save the file and run it again. Now, when you run the batch file, the window will stay open while displaying "Press any key to continue…" Once you press a key, the window will close.[2]
  5. Advertisement
Section 4 of 4:

Other Ways to Run Batch Files

Download Article
  1. How.com.vn English: Step 1 Double-click any .BAT file to run it instantly.
    If you don't want to use the Command Prompt, you can easily run a batch file from any location on your Windows PC just by double-clicking it.
    • If double-clicking the batch file opens and closes a window without showing you any details, see this troubleshooting tip.
  2. How.com.vn English: Step 2 Use the Run dialog.
    To call up a batch script from the Run dialog:
    • Press Windows key + R to open Run.
    • Click Browse.
    • Navigate to and select your batch file.
    • Click Open.
    • Click OK to run the batch file.
  3. How.com.vn English: Step 3 Run the batch file at a specific time and date.
    Use Windows Task Scheduler to automatically run a batch file at the time and date of your choice. You can have the batch file run once or on a regular schedule.
    • Press the Windows key and type task scheduler.
    • Click Task Scheduler.
    • Expand the "Task Scheduler Library" folder.
    • Create a new folder: Right-click Task Scheduler Library, click New Folder…, type a name, and click OK.
    • Right-click your new folder and select Create basic task. Type a name for your task and click Next.
    • Choose when (and how often) you want the batch file to run. For example, if you want it to run at a certain date, select the date. Then, click Next.
    • Select Start a program, click Next, and then click Browse to select your .BAT file.
    • Click Finish. The batch file will now run automatically at the selected time and date.
  4. How.com.vn English: Step 4 Run the batch file automatically when Windows starts.
    If you want the batch file to start automatically when your PC boots into Windows, you can copy or move the file to your Windows startup scripts folder. Here's how:
    • Press Windows key + R to open the Run dialog.
    • Type shell:startup and click OK. This opens a File Explorer window to your Startup folder.
    • Drag or copy the .BAT file into the folder.
  5. Advertisement

Expert Q&A

Search
Add New Question
  • Question
    After I run the batch script, it just opens another line and nothing happens.
    How.com.vn English: Nicole Levine, MFA
    Nicole Levine, MFA
    How.com.vn Technology Writer
    Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
    How.com.vn English: Nicole Levine, MFA
    How.com.vn Technology Writer
    Expert Answer
    Many batch scripts won't display any confirmation that the commands have run. If you wrote the batch script yourself, you can switch the value of "echo" to "on" to display the commands at the prompt as they run. You can also add "pause" to the end of the script to make it prompt the user to press a key to continue. But in general, as long as you don't see an error, that means the batch script executed properly.
  • Question
    What does this error mean? '.' is not recognized as an internal or external command, operable program or batch file.
    How.com.vn English: How.com.vn Staff Editor
    This answer was written by one of our trained team of researchers who validated it for accuracy and comprehensiveness.
    How.com.vn English: How.com.vn Staff Editor
    How.com.vn Staff Editor
    Staff Answer
    The batch script is trying to run "." as a command, which is not a recognized command. Look for the extra period somewhere in the script and remove it.
  • Question
    How do I get out after I run my batch file ?
    How.com.vn English: How.com.vn Staff Editor
    This answer was written by one of our trained team of researchers who validated it for accuracy and comprehensiveness.
    How.com.vn English: How.com.vn Staff Editor
    How.com.vn Staff Editor
    Staff Answer
    Just click the '''X''' at the top corner of the command prompt window to close it. Or, if the batch script is hung on a process and not letting you go to the next line, you can press Ctrl + C to stop the batch file from running.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      Warnings

      • Never run a .BAT file as an administrator unless you know exactly what the file does. If the batch file is malicious, running it as an administrator could steal your data, install malware and viruses, and other dirty deeds.
      • Be careful when downloading .BAT files from the web or as email attachments. Make sure your PC is always protected by Microsoft Defender (or your preferred antivirus software) before running files from unknown sources.
      Advertisement

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 473,933 times.
      How helpful is this?
      Co-authors: 4
      Updated: April 3, 2024
      Views: 473,933
      Categories: Command Prompt
      Article SummaryX

      1.Press the Windows + R keys.
      2.Click Browse.
      3.Select the .bat file.
      4.Click Open.
      5.Click OK.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 473,933 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement