How to Create a Batch File in Windows 10

Automate tasks and more with batch files

What To Know

  • Create a batch file in Windows 10 by typing your commands in a blank Notepad document, and saving it as .bat instead of .txt.
  • Commands include PAUSE, COPY, and CLS (clear).
  • To add comments, start a line with two colons and a space. Comments are useful to divide up a batch file into sections.

This article explains how to create a batch file in Windows 10 using the Notepad, how to add comments, and includes a list of common commands.

How to Create a Batch File in Windows 10

Creating a batch file in Windows 10 is as simple as typing the commands you want to run into a blank notepad document, then saving the document as a .bat file instead of a text document. You can then run the file by clicking on it, which will automatically launch the Windows command shell and execute your commands.

Here's how to create a simple batch file in Windows 10:

  1. Type Notepad into the search bar, and click the Notepad app when it appears in the results.

    A screenshot of opening Notepad in Windows 10.
  2. Type the following into a blank Notepad document to create a simple batch file:

    @ECHO OFF
    ECHO If you're seeing this text, you've successfully created your first batch file in Windows 10. Congratulations!

    PAUSE
    A batch file in Notepad.
  3. Click File in the upper left corner of the Notepad window.

    A batch file written in Notepad.
  4. Click Save as in the dropdown menu.

    Saving a batch file in Notepad.
  5. Type a name for the script, like test.bat, and click Save.

    Naming a batch file in Notepad.

    Make note of the location on your hard drive where the file is saved, as that's where you'll be able to find and execute it in the future.

  6. Locate the file you just saved, and double click it.

    A batch file on the Windows 10 desktop.
  7. If the file was created correctly, you will see a command window that looks like this:

    An executed batch file in Windows 10.

How to Create More Advanced Batch Files

A batch file is a special type of file that automatically opens a command window when activated. If you already know the commands that you need your file to execute, then you're ready to go. Simply type the commands into Notepad in the manner outlined above, save as a .bat file, and open the batch file to execute the commands whenever you want.

If you're not sure what to put in your file, keep in mind that a batch file is essentially an ordered list of commands that will execute via the Windows command prompt. Anything you could type manually into the command prompt, you can put in a batch file. The file will then execute each command, in order, from the top to the bottom.

Here are some useful commands to use in batch files, along with explanations of what they do:

  • @ECHO OFF: Disables display of the prompt. This is usually used at the beginning of a batch file for a cleaner display. You don't need the @, but including it hides the ECHO OFF command as well.
  • ECHO: Prints the following text to the command window.
  • PAUSE: Causes the command window to remain open after the batch file is finished, or allows text in the window to be read before proceeding.
  • TITLE: Places a custom title in the title bar of the command window.
  • CLS: Clears the command window.
  • EXIT: Exits and closes the command window.
  • COPY: Copy one or more files.
  • REM: Record comments or remarks.
  • IPCONFIG: Display detailed IP information for each network adapter connected to your system.
  • PING: Sends an Internet Control Message Protocol (ICMP) echo request to an IP address or website.
  • TRACERT: Check your connection to an IP or website using ICMP.
  • SET: Used to set variables.
  • IF: Perform a conditional function based on user input or another variable.

Inserting Comments Into Batch Files

If you start a line in your batch file with two colons and a space, it won't be executed. This allows you to easily insert comments into your batch file. Comments are useful to divide up a batch file into sections with a brief explanation as to the purpose of the section.

Here's an example of a batch file with comments:

@ECHO OFF
:: This batch file is just an example to show how comments work.
TITLE Just a basic hello world example to show how comments work.
ECHO Hello world!
:: This is another comment, you won't see me unless you read the batch file!
ECHO Goodbye!
PAUSE

If you paste those commands into a batch file and run it, you'll see an output like this:

A batch file showing how comments are hidden.

Comments aren't necessary, but it is a useful option that you'll tend to need more when creating complicated batch files with lots of sections.

Here's a slightly more complicated batch file that uses a variety of commands, comments, and actually performs a useful task:

:: This batch file is designed to check for internet connectivity.
@ECHO OFF
TITLE Internet Status and Connectivity Checker
:: This command shows your network details.
ipconfig /all
PAUSE
:: This section checks to see if a specific website is available.
ping google.com
:: This section lets you choose whether or not to run tracert.
set "reply=y"
set /p "reply=Run traceroute now? [y|n]: "
if /i not "%reply%" == "y" goto :eof
tracert google.com
PAUSE

This file checks your internet connection using ipconfig and then pauses so you can examine it. It then pings google.com. Finally, it gives you the option to run the tracert command if you want. It then pauses a second time, allowing you to check the results before closing the window.

The final result looks like this:

A batch file example output.

You can use any command prompt commands you like in a batch file, including variables and user interaction like the above example, writing information to other files, and more.