How to run bat file in cmd windows

Download Article

Easily run .BAT files in Windows and troubleshoot common problems

Download Article

  • Running from the Batch File’s Folder
  • |

  • Running from Anywhere
  • |

  • Troubleshooting
  • |

  • Other Ways to Run Batch Files
  • |

  • Video
  • |

  • Expert Q&A
  • |

  • Warnings

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 wikiHow 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.
  1. 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.[1]

    • 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. 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. 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. 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

  1. 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\wikiHow\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. 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.[2]

    • 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. 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\wikiHow\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. 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

  1. 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.[3]
    • 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. 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. 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.[4]
  5. Advertisement

  1. 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. 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. 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. 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:[5]

    • 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

Add New Question

  • Question

    After I run the batch script, it just opens another line and nothing happens.

    Nicole Levine is a Technology Writer and Editor for wikiHow. 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.

    wikiHow 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.

    This answer was written by one of our trained team of researchers who validated it for accuracy and comprehensiveness.

    wikiHow 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 ?

    This answer was written by one of our trained team of researchers who validated it for accuracy and comprehensiveness.

    wikiHow 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

Thanks for submitting a tip for review!

  • 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

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 593,848 times.

Is this article up to date?

To run a batch file in the Command Prompt (cmd), simply navigate to the directory containing the batch file using the `cd` command, and then enter the file name followed by `.bat`.

Here’s an example:

cd C:\Path\To\Your\BatchFile
yourfile.bat

What is a Batch File?

A batch file is a simple text file that contains a series of commands that the Command Prompt (CMD) in Windows can execute sequentially. These files have a `.bat` extension and are primarily used to automate tasks. Common uses for batch files include automating repetitive tasks, configuring settings, and executing multiple commands at once without requiring user intervention.


How to Run XML File in Cmd: A Quick Guide

How to Run XML File in Cmd: A Quick Guide

Preparing Your Batch File

Creating a Batch File

Creating a batch file is straightforward. You can use any text editor, like Notepad, to write your commands.

  1. Open Notepad or your preferred text editor.
  2. Write commands that you want the batch file to execute. For instance, here’s a simple batch file that displays «Hello, World!» on the screen and waits for the user to press a key:
    @echo off
    echo Hello, World!
    pause
    

Saving Your Batch File

Once you’ve written your commands, it’s essential to save the file correctly to ensure it runs as expected. When saving:

  • Choose File > Save As.
  • In the save dialog, under the «Save as type» dropdown, select «All Files».
  • Name your file with a `.bat` extension, for instance, `example.bat`.

By following these steps, you ensure that your batch file is properly created and saved for execution.


How to Run Py File in Cmd: A Quick Guide

How to Run Py File in Cmd: A Quick Guide

Running a Batch File from CMD

Opening the Command Prompt

Before you can run a batch file, you need to open the Command Prompt. There are several methods to do this:

  • Search in the Start Menu: Type «cmd» or «Command Prompt» in the search box and select it.
  • Using Run Command: Press `Win + R`, type `cmd`, and hit Enter.

Command Syntax to Run a Batch File

To execute a batch file from CMD, you’ll need to use the correct syntax. Typically, you would provide the full path to the batch file:

C:\path\to\your\file.bat

It’s crucial to ensure that you format the path correctly, or else CMD won’t find your batch file.


How to Run a EXE in Cmd: A Quick Guide

How to Run a EXE in Cmd: A Quick Guide

Using the Full Path

The easiest way to run a batch file is by using its full path. For instance, if your batch file is located on your desktop, the command would look like this:

C:\Users\YourName\Desktop\example.bat

Changing Directory First

Alternatively, you could navigate to the directory where your batch file is saved before executing it. Here’s how:

  1. Change the directory in CMD to where the batch file is located:
    cd C:\Users\YourName\Desktop
    
  2. After changing the directory, run the batch file by simply typing its name:
    example.bat
    

This method can be more convenient when you are working in a directory with multiple batch files.


How to Run Check Disk in Cmd: A Simple Guide

How to Run Check Disk in Cmd: A Simple Guide

Additional Methods to Run a Batch File

Running Batch Files from Different Locations

If you frequently use batch files located in different directories, you can simplify access by adding the directory of your batch files to the `PATH` environment variable. This way, you can execute the batch files from any command prompt window without navigating to their respective locations.

Executing a Batch File with Parameters

Batch files can also accept parameters when they are run. This allows you to customize how the batch file behaves based on the input it receives. Here’s an example showing how to utilize parameters in a batch file:

@echo off
echo The first parameter is %1

If you run this batch file with a parameter like so:

example.bat Hello

It would output: «The first parameter is Hello». This capability makes batch files flexible for various tasks.


How to Run Python in Cmd: A Quick Guide

How to Run Python in Cmd: A Quick Guide

Troubleshooting Common Issues

Common Errors When Running Batch Files

When executing batch files, you might encounter some common errors:

  • File Not Found: This usually occurs if the path to the batch file is incorrect. Double-check your syntax.
  • Permission Errors: Sometimes, batch files require elevated privileges. Running CMD as an administrator can resolve these issues.

Tips for Successful Execution

To improve the success of running batch files:

  • Check File Paths: Always ensure that file paths are accurate, particularly if you’re using relative paths.
  • Permissions: Make sure you have the necessary permissions to access the directory and execute the batch file. If needed, right-click the CMD application and choose «Run as administrator».

How to Open a File on Cmd: A Quick Guide

How to Open a File on Cmd: A Quick Guide

Conclusion

Understanding how to run a batch file in CMD is a powerful skill that allows you to automate tasks and enhance productivity on your Windows machine. By following the steps outlined above, you can easily create, save, and execute batch files to fit your needs.


How to Display Files in Cmd: A Quick Guide

How to Display Files in Cmd: A Quick Guide

Frequently Asked Questions (FAQ)

What is the difference between a batch file and a script?

Batch files are specific to Windows and utilize CMD to execute commands, while scripts can refer to various file types (like PowerShell or Python scripts) that can be run in different environments. The core concept remains similar: both automate tasks through written commands.

Can I run a batch file without using CMD?

Yes, you can double-click the batch file in Windows Explorer to execute it. However, running it through CMD provides better control and visibility into any output or errors.

How do I run multiple batch files from a single command?

You can create one master batch file that calls other batch files in sequence. For instance:

@echo off
call first.bat
call second.bat

This setup helps manage multiple tasks easily.

Is it safe to run batch files downloaded from the internet?

Exercise caution when running downloaded batch files. Always verify the source and, if possible, inspect the file contents to ensure it doesn’t contain harmful commands.


By practicing the creation and execution of batch files, you can gain a solid understanding of CMD and enhance your automation capabilities in Windows.

  1. Method 1: Running Batch Files Directly in CMD

  2. Method 2: Running Batch Files Using Python

  3. Method 3: Running Batch Files with Command Line Arguments

  4. Conclusion

  5. FAQ

How to Run Batch (.bat) File in CMD

Running a Batch (.bat) file in Command Prompt (CMD) is a fundamental skill for Windows users, especially for those who want to automate tasks or execute scripts efficiently. Whether you’re a beginner or have some experience with command-line operations, this tutorial will guide you through the process of running Batch files seamlessly. We’ll cover different methods, including how to do this using Python, making it easier for you to integrate Batch file execution into your workflows. By the end of this article, you will have a clear understanding of how to run Batch files in CMD, along with practical code examples to enhance your skills.

Method 1: Running Batch Files Directly in CMD

The simplest way to run a Batch file is to execute it directly from the Command Prompt. To do this, you need to know the path of your Batch file. Here’s how to do it:

  1. Open Command Prompt by typing “cmd” in the Windows search bar and hitting Enter.
  2. Navigate to the directory containing your Batch file using the cd command. For example, if your Batch file is located in the “Scripts” folder on your desktop, you would type:
cd C:\Users\YourUsername\Desktop\Scripts
  1. Once you’re in the correct directory, type the name of your Batch file, including the .bat extension, and press Enter:

Output:

Batch file is executed successfully.

This method is straightforward and doesn’t require any additional software. It’s perfect for quick executions or testing scripts. Just ensure that you have the correct path and file name. If your Batch file contains commands that require administrative privileges, make sure to run CMD as an administrator by right-clicking on the Command Prompt icon and selecting “Run as administrator.”

Method 2: Running Batch Files Using Python

If you’re looking to integrate Batch file execution into a Python script, you can easily do this using the subprocess module. This method is particularly useful for developers who want to automate tasks or incorporate Batch files into larger applications. Here’s a simple example:

import subprocess

subprocess.run(["C:\\Path\\To\\YourBatchFile.bat"])

In this code, we import the subprocess module, which is a powerful tool for spawning new processes and connecting to their input/output/error pipes. The run() function is then used to execute the specified Batch file. Make sure to replace C:\\Path\\To\\YourBatchFile.bat with the actual path of your Batch file.

Output:

Batch file executed from Python.

This method allows you to run Batch files as part of a larger Python program, which can be very handy when you want to perform multiple tasks in sequence or manage file operations programmatically. The subprocess module provides additional functionalities, such as capturing output and handling errors, making it a versatile choice for script automation.

Method 3: Running Batch Files with Command Line Arguments

Sometimes, you may want to pass arguments to your Batch file when executing it. This can be done easily from CMD or within a Python script. Here’s how you can do it in both scenarios.

Using CMD

You can pass arguments directly in CMD by adding them after the Batch file name. For example:

YourBatchFile.bat arg1 arg2

Output:

Arguments received: arg1, arg2

Using Python

To run a Batch file with arguments in Python, you can modify the subprocess.run() function like this:

import subprocess

subprocess.run(["C:\\Path\\To\\YourBatchFile.bat", "arg1", "arg2"])

In this example, arg1 and arg2 are passed to the Batch file as command-line arguments. Inside your Batch file, you can access these arguments using %1, %2, etc.

Output:

Arguments received: arg1, arg2

This method is particularly useful when you need to customize the execution of your Batch file based on user input or other dynamic data. It opens up a range of possibilities for making your scripts more interactive and functional.

Conclusion

Running Batch files in CMD is a valuable skill that can enhance your productivity and streamline various tasks on your Windows system. Whether you choose to execute them directly in Command Prompt or integrate them into a Python script, the methods we’ve discussed provide flexibility and efficiency. By understanding how to run Batch files and pass arguments, you can automate repetitive tasks and manage system operations effectively. With practice, you’ll become proficient in using Batch files, making your workflow smoother and more organized.

FAQ

  1. how do I create a Batch file?
    You can create a Batch file by opening Notepad, writing your commands, and saving the file with a .bat extension.

  2. can I run a Batch file from a USB drive?
    Yes, you can run a Batch file from a USB drive by navigating to the drive in CMD and executing the file.

  3. what if my Batch file doesn’t execute?
    Ensure that the file path is correct and that you have the necessary permissions to run the file.

  4. can I schedule a Batch file to run automatically?
    Yes, you can use Windows Task Scheduler to schedule Batch files to run at specific times or events.

  5. how can I debug a Batch file?
    You can debug a Batch file by adding echo statements to print variable values or using pause to stop execution at certain points.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

    Quick Links:

  • Introduction
  • What is a Batch File?
  • Setting Up Your Environment
  • Creating a Batch File
  • Running a Batch File from CMD
  • Common Issues & Troubleshooting
  • Advanced Uses of Batch Files
  • Case Studies & Examples
  • Expert Insights
  • FAQs

Introduction

Running a Windows batch file from the command line (CMD) is an essential skill for users looking to automate repetitive tasks, manage system processes, or streamline workflows. This guide will take you through the steps necessary to create, modify, and execute batch files effectively.

What is a Batch File?

A batch file is a text file containing a series of commands that are executed by the Windows command line interpreter. The file extension typically used for batch files is .bat. When a batch file is run, it executes each command in the order they are written, making it a powerful tool for automation.

Characteristics of Batch Files

  • Simple to create and edit using any text editor.
  • Can execute multiple commands in sequence.
  • Supports conditional statements and loops.
  • Useful for automating repetitive tasks.

Setting Up Your Environment

Before you can run batch files from CMD, ensure your environment is properly set up:

1. Open Command Prompt

To open CMD, press Win + R, type cmd, and hit Enter.

2. Set the Path (Optional)

If your batch files are located in a specific directory, you can add that directory to your system’s PATH variable. This allows you to run the batch files from any location in CMD without specifying the full path.

How to Add a Directory to PATH

  1. Right-click on This PC and select Properties.
  2. Click on Advanced system settings.
  3. In the System Properties window, click on Environment Variables.
  4. Under System variables, find Path, and click Edit.
  5. Add your directory path and click OK.

Creating a Batch File

Now that your environment is set up, let’s create a simple batch file.

Step-by-Step Guide to Create a Batch File

  1. Open Notepad or any text editor.
  2. Type the following commands:
  3. @echo off
    echo Hello, World!
    pause
            
  4. Save the file with a .bat extension, e.g., hello.bat.

Running a Batch File from CMD

To run the batch file you just created, follow these steps:

Step-by-Step Execution

  1. Open Command Prompt.
  2. Navigate to the directory where your batch file is located using the cd command. For example:
  3. cd C:\path\to\your\batchfile
  4. Type the name of the batch file and hit Enter:
  5. hello.bat

You should see «Hello, World!» printed in the command prompt.

Common Issues & Troubleshooting

When running batch files, you may encounter some common issues:

1. Command Not Found

This usually occurs if the command is misspelled or the executable is not in the system’s PATH.

2. Permission Denied

If your batch file tries to access files or folders that require administrative privileges, run CMD as an administrator.

Advanced Uses of Batch Files

Batch files can do much more than just echo messages. Here are some advanced uses:

Automating System Tasks

Batch files can automate tasks like disk cleanup, software installations, and backups. For example:

@echo off
echo Cleaning temporary files...
del /q /f C:\Windows\Temp\*
    

Scheduling Batch Files

Use Windows Task Scheduler to run batch files at specific intervals. This can be useful for backups or system maintenance.

Case Studies & Examples

Here are a couple of examples illustrating the power of batch files:

Example 1: Daily Backup Script

A business needed to back up its data daily. They created a batch file that copied files from their main directory to an external hard drive. The script looked like this:

@echo off
xcopy C:\Users\YourName\Documents D:\Backup\Documents /E /I
    

Example 2: System Cleanup

A user wanted to automate system cleanup. They created a batch file to delete temporary files and clear cache:

@echo off
del /q /f C:\Users\YourName\AppData\Local\Temp\*
rd /s /q C:\Windows\Temp
    

Expert Insights

According to industry experts, batch files remain relevant despite the rise of more sophisticated scripting languages. They are lightweight, easy to learn, and effective for simple automation tasks.

Experts recommend keeping batch files organized and well-documented to avoid confusion, especially in a corporate environment.

FAQs

1. What is the difference between a batch file and a script?

A batch file is specific to Windows and uses the command interpreter, while scripts can refer to any programming language, including Python, JavaScript, and more.

2. Can I run a batch file from another batch file?

Yes, you can call another batch file within a batch file using its name.

3. Are there any risks associated with running batch files?

Yes, batch files can execute harmful commands. Always review the content before running unknown batch files.

4. Can I schedule a batch file to run automatically?

Yes, you can use Windows Task Scheduler to schedule batch files to run at specific times or intervals.

5. What command is used to pause a batch file?

The command pause is used to pause execution and wait for user input to continue.

6. How do I make a batch file executable from anywhere?

Add the folder containing your batch file to the system PATH variable.

7. Can batch files run PowerShell scripts?

Yes, you can call PowerShell scripts from within a batch file using the powershell command.

8. What is the purpose of @echo off?

This command prevents the commands in the batch file from being displayed in the command prompt during execution.

9. Can I pass arguments to a batch file?

Yes, you can pass arguments to a batch file and access them using %1, %2, etc.

10. Where can I find more resources on batch scripting?

Websites like Computer Hope and SS64 provide comprehensive resources on batch scripting.

Random Reads

  • How to seal a leaking radiator
  • How to seal around basin bathtub silicone
  • How to organize chrome bookmarks
  • How to organize keys
  • How to organize a kitchen
  • How to delete run history in windows
  • How to get the most points at tetris
  • How to get the three regis in pokemon emerald
  • How to paint a house
  • How to paint a cane chair

Windows 10 run batch file

(Image credit: Future)

On Windows 10, a batch file typically has a «.bat» extension, and it is a special text file that contains one or multiple commands that run in sequence to perform various actions with Command Prompt.

Although you can type commands manually to execute a particular task or change system settings on Windows 10, a batch file simplifies the work of having to re-type the commands, saving you time and avoiding mistakes.

You can also use other tools like PowerShell to write even more advanced scripts. However, running batch files in Command Prompt is still relevant for executing commands to change settings, automate routines, and launch apps or web pages on your computer.

This guide will walk you through the steps to create and run a batch file on Windows 10. Also, we will outline the steps to create advanced scripts and rum them automatically on schedule using the Task Scheduler.

How to create a batch file on Windows 10

The process of writing a batch file is not complicated. You only need Notepad or another text editor and some basic knowledge of typing commands in Command Prompt. These instructions will help you create a basic and advanced batch file to query system settings.

Create basic Windows 10 batch file

To create a basic batch file on Windows 10, use these steps:

All the latest news, reviews, and guides for Windows and Xbox diehards.

  1. Open Start.
  2. Search for Notepad and click the top result to open the text editor.
  3. Type the following lines in the text file to create a batch file: 

@ECHO OFF

ECHO Hello World! Your first batch file was printed on the screen successfully. 

PAUSE

The above script outputs the phrase, «Hello World! Your first batch file was printed on the screen successfully,» on the screen.

  • @ECHO OFF — Shows the message on a clean line disabling the display prompt. Usually, this line goes at the beginning of the file. (You can use the command without the «@» symbol, but it’s recommended to include it to show a cleaner return.)
  • ECHO — The command prints the text after the space on the screen.
  • PAUSE — Allows the window to stay open after the command has been executed. Otherwise, the window will close automatically as soon as the script finishes executing. You can use this command at the end of the script or after a specific command when running multiple tasks and wanting to pause between each line.

Windows 10 basic batch file

(Image credit: Future)
  1. Click the File menu.
  2. Select the Save as option.
  3. Confirm a name for the script — for example, first_basic_batch.bat.
  • Quick note: While batch files typically use the .bat file extensions, you can also find them using the .cmd or .btm file extensions.

Once you complete the steps, double-click the file to run it. Alternatively, you can use the steps below to learn how to run a batch file with Command Prompt, File Explorer, or Task Scheduler.

Create advanced Windows 10 batch file

To create an advanced Windows batch file with multiple commands, use these steps:

  1. Open Start.
  2. Search for Notepad and click the top result to open the text editor.
  3. Type the following lines in the text file to create a more advanced Windows 10 batch file:

@ECHO OFF 

:: This batch file details Windows 10, hardware, and networking configuration.

TITLE My System Info

ECHO Please wait… Checking system information.

:: Section 1: Windows 10 information

ECHO ==========================

ECHO WINDOWS INFO

ECHO ============================

systeminfo | findstr /c:»OS Name»

systeminfo | findstr /c:»OS Version»

systeminfo | findstr /c:»System Type»

:: Section 2: Hardware information.

ECHO ============================

ECHO HARDWARE INFO

ECHO ============================

systeminfo | findstr /c:»Total Physical Memory»

wmic cpu get name

wmic diskdrive get name,model,size

wmic path win32_videocontroller get name

wmic path win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution

:: Section 3: Networking information.

ECHO ============================

ECHO NETWORK INFO

ECHO ============================

ipconfig | findstr IPv4ipconfig | findstr IPv6

START https://support.microsoft.com/en-us/windows/windows-10-system-requirements-6d4e9a79-66bf-7950-467c-795cf0386715

PAUSE

The above script runs each line to query a series of system details, and the result will be divided into three categories, including «WINDOWS INFO,» «HARDWARE INFO,» and «NETWORK INFO.» Also, the «START» command will open the web browser in the official support page outlining the Windows 10 system requirements, which you can check against your information.

  • @ECHO OFF — Shows the message on a clean line disabling the display prompt. Usually, this line goes at the beginning of the file.
  • TITLE — Prints a custom name in the title bar of the console window.
  • :: — Allows writing comments and documentation information. These details are ignored when the system runs the batch file.
  • ECHO — Prints the text after the space on the screen.
  • START — Opens an app or website with the default web browser.
  • PAUSE — Tells the console window to stay open after running the command. If you do not use this option, the window will close automatically as soon as the script finishes executing.

Advanced script sample

(Image credit: Future)
  1. Click the File menu.
  2. Select the Save as option.
  3. Type a name for the script — for example, first_advanced_batch.bat.

After you complete the steps, double-click the .bat file to run it or use the steps below to execute the script with Command Prompt, File Explorer, or Task Scheduler.

Create actionable Windows 10 batch file

You can also write batch scripts for any task that does not require user interaction. For instance, to map a network drive, install an application, change system settings, and more.

To create a non-interactive batch file on Windows 10, use these steps:

  1. Open Start.
  2. Search for Notepad and click the top result to open the text editor.
  3. Type the following command to map a network drive in the text file: net use z: \\PATH-NETWORK-SHARE\FOLDER-NAME /user:YOUR-USERNAME YOUR-PASSWORD

In the command, replace the «\\PATH-NETWORK-SHARE\FOLDER-NAME» for the folder network path to mount on the device and «YOUR-USERNAME YOUR-PASSWORD» with the username and password that authenticates access to the network share. 

This example maps a network folder as a drive inside File Explorer using the «Z» drive letter: net use z: \\10.1.4.174\ShareFiles

  • Quick note: If you are accessing the files from another computer that uses a specific username and password, do not forget to use the /user: option with the correct credentials.

Map network drive script

(Image credit: Future)
  1. Click the File menu.
  2. Select the Save as option.
  3. Confirm a name for the script — for example, mount-z-network-drive.bat.

Once you complete the steps, the batch file will map the network folder without opening a Command Prompt window.

We only demonstrate a script with a single command, but you can include as many as you like, as long as you write them one per line.

How to run a batch file on Windows 10

Windows 10 has at least three ways to write batch files. You can run them on-demand using Command Prompt or File Explorer. You can configure the script using the Task Scheduler app to run it on schedule. Or you can save the batch files in the «Startup» folder to let the system run them as soon as you sign into the account.

Run batch file on-demand

If you want to run a script on-demand, you can use File Explorer or Command Prompt.

Command Prompt

To run a script file with Command Prompt on Windows 10, use these steps:

  1. Open Start.
  2. Search for Command Prompt, right-click the top result, and select the Run as administrator option.
  3. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME.bat

In the command, make sure to specify the path and name of the script. 

This example runs the batch file located in the «scripts» folder inside the «Downloads» folder: C:\Users\UserAccount\Downloads\first_basic_batch.bat

Run batch file from Command Prompt

(Image credit: Future)

After you complete the steps, the console will return the results, and the window won’t close even if the script does not include the «PAUSE» command since you are invoking the script from within a console session that was already open.

File Explorer

To run a batch file with File Explorer, use these steps:

  1. Open File Explorer.
  2. Browse to the folder with the batch file.
  3. Double-click the script file to run it.
  4. (Optional) If a command in the batch file requires administrator privileges, you will have to run the script as an admin by right-clicking the file and selecting the Run as administrator option.

File Explorer run batch file as administrator

(Image credit: Future)
  1. Click the Yes button

Once you complete the steps, the script will run each command in sequence, displaying the results in the console window.

Run batch files on startup

Windows 10 also features a known folder called «Startup,» which the system checks every time it starts to run applications, shortcuts, and scripts automatically without the need for extra configuration.

To run a script on the Windows 10 startup, use these steps:

  1. Open File Explorer.
  2. Open the folder containing the batch file.
  3. Right-click the batch file and select the Copy option.
  4. Use the Windows key + R keyboard shortcut to open the Run command.
  5. Type the following command: shell:startup
(Image credit: Future)
  1. Click the OK button.
  2. Click the Paste option from the «Home» tab in the Startup folder. (Or click the Paste shortcut button to create a shortcut to the batch file.)

Configure script on startup folder

(Image credit: Future)

After you complete the steps, the batch file will execute automatically every time you log into your account.

Run batch file with Task Scheduler

To use Task Scheduler to run the batch file automatically at a specific time, use these steps:

  1. Open Start.
  2. Search for Task Scheduler and click the top result to open the app.
  3. Right-click the «Task Scheduler Library» branch and select the New Folder option.
  4. Confirm a name for the folder — for example, MyScripts.
  • Quick note: You don’t need to create a folder, but keeping the system and your tasks separate is recommended.
  1. Click the OK button.
  2. Expand the «Task Scheduler Library» branch.
  3. Right-click the MyScripts folder.
  4. Select the Create Basic Task option.

Task Scheduler create basic task

(Image credit: Future)
  1. In the «Name» field, confirm a name for the task — for example, SystemInfoBatch.
  2. (Optional) In the «Description» field, write a description for the task.
  3. Click the Next button.
  4. Select the Monthly option.
  • Quick note: Task Scheduler lets you choose from different triggers, including a specific date, during startup, or when a user logs in to the computer. In this example, we will select the option to run a task every month, but you may need to configure additional parameters depending on your selection.

Task trigger settings

(Image credit: Future)
  1. Click the Next button.
  2. Use the «Start» settings to confirm the day and time to run the task.
  3. Use the «Monthly» drop-down menu to pick the months of the year to run the task.

Task Scheduler date selection

(Image credit: Future)
  1. Use the «Days» or «On» drop-down menu to confirm the days to run the task.

Schedule batch file day of the month

(Image credit: Future)
  1. Click the Next button.
  2. Select the Start a program option to run the batch file.

Start a program action

(Image credit: Future)
  1. In the «Program/script» field, click the Browse button.
  2. Select the batch file you want to execute.

Task Scheduler batch file location

(Image credit: Future)
  1. Click the Finish button.

Once you complete the steps, the task will run the script during the configured time and date or action.

The above instructions are meant to schedule only a basic task. You can use these instructions to create a more customizable task with the Task Scheduler.

This guide focuses on Windows 10, but the same steps will also work for older versions, including Windows 8.1 and 7. Also, you can refer to these instructions if you have Windows 11 installed on your computer.

More Windows resources

For more helpful articles, coverage, and answers to common questions about Windows 10 and Windows 11, visit the following resources:

  • Windows 11 on Windows Central — All you need to know
  • Windows 10 on Windows Central — All you need to know

Mauro Huculak has been a Windows How-To Expert contributor for WindowsCentral.com for nearly a decade and has over 15 years of experience writing comprehensive guides. He also has an IT background and has achieved different professional certifications from Microsoft, Cisco, VMware, and CompTIA. He has been recognized as a Microsoft MVP for many years.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • При загрузке windows рябит экран
  • Как переместить файлы с mac на windows
  • Asus n73s драйвера windows 10 usb
  • Аналог garageband для windows
  • Отсутствует файл windows system32 winload exe