How to Create a Batch File in Windows 10

How to Create a Batch File in Windows 10

How to Create a Batch File in Windows 10 GA S REGULAR Menu Lifewire Tech for Humans Newsletter! Search Close GO Software & Apps > Windows

How to Create a Batch File in Windows 10

Automate tasks and more with batch files

By Jeremy Laukkonen Jeremy Laukkonen Writer Shoreline Community College Jeremy Laukkonen is automotive and tech writer for numerous major trade publications. When not researching and testing computers, game consoles or smartphones, he stays up-to-date on the myriad complex systems that power battery electric vehicles . lifewire's editorial guidelines Updated on January 12, 2021 Reviewed by Ryan Perian Reviewed by Ryan Perian Western Governors University Ryan Perian is a certified IT specialist who holds numerous IT certifications and has 12+ years' experience working in the IT industry support and management positions. lifewire's editorial guidelines Tweet Share Email Tweet Share Email Windows The Ultimate Laptop Buying Guide

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: Type Notepad into the search bar, and click the Notepad app when it appears in the results. 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 Click File in the upper left corner of the Notepad window. Click Save as in the dropdown menu. Type a name for the script, like test.bat, and click Save. 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. Locate the file you just saved, and double click it. If the file was created correctly, you will see a command window that looks like this:

Batch File Commands and Descriptions

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: 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? [yn]: "
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: 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. Was this page helpful? Thanks for letting us know! Get the Latest Tech News Delivered Every Day Subscribe Tell us why! Other Not enough details Hard to understand Submit More from Lifewire How to Delete Temporary Files in Windows How to Open an Elevated Command Prompt in Windows Net Send Command (Examples, Switches, and More) How to Map a Network Drive in Windows 11 BAT File (What It Is and How to Open One) How to Use the Netstat Command How to Open Command Prompt (Windows 11, 10, 8, 7, etc.) How to Access Device Manager From the Command Prompt The 4 Best Free Text Editors for Windows & Mac How to Redirect Command Prompt Output to a File What Is a Redirection Operator? List of Windows 7 Command Prompt Commands How to Verify File Integrity in Windows With FCIV How to Install PIP on Windows DO File (What It Is & How to Open One) Command Prompt: What It Is and How to Use It Newsletter Sign Up Newsletter Sign Up Newsletter Sign Up Newsletter Sign Up Newsletter Sign Up By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookies Settings Accept All Cookies
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!