How to Write a Batch Script on Windows

Posted by Reinaldo Massengill on Monday, August 26, 2024

Quick Links

Key Takeaways

  • Batch files are a list of commands executed when double-clicked. They work on modern versions of Windows and are created using a plain text editor.
  • Simple batch files can be created by typing commands line by line. Adding comments and hiding commands with "ECHO OFF" can improve readability.
  • Batch files can be used for more complex tasks, such as saving command outputs to a text file or running non-interactively to perform multiple tasks. They can also include conditional statements for greater control.

Do you know how to use the Command Prompt? If you do, you can write a batch file. In its simplest form, a batch file (or batch script) is a list of several commands that are executed when you double-click the file. Batch files go all the way back to DOS, but still work on modern versions of Windows.

How to Make a Basic Batch File

A batch file is simply a text file saved with the .bat file extension. You can write one using Notepad or a more advanced plain text editor like Notepad++ or Visual Studio Code (VSCode), but don't use a word processor like Microsoft Word. Word processors usually insert additional stuff related to formatting that will prevent your batch script from executing correctly.

PowerShell scripts and Bash scripts may be more powerful, but batch files can still be plenty useful if you need to run basic Windows commands. They still work on both Windows 10 and Windows 11, and will for the foreseeable future.

Let's create a simple batch file. First, open Notepad. Type the following lines into it:

ECHO OFF
ECHO Hello World
PAUSE

Next, save the file by clicking File > Save. Give it any name you like, but replace the default .txt file extension with the .bat extension.

For example, you might want to name it hello_world.bat .

You now have a batch file with the .bat file extension. Double-click it to run it. This particular batch file sets ECHO off (which cleans up the output by hiding the commands from being printed at the prompt, prints the text "Hello World" to the screen, and then waits for you to press a key before it ends.

If you didn't add PAUSE to the file, the batch file would simply run its commands and then automatically close. In this case, it would print "Hello World" to the window and then immediately close the Command Prompt window. When you want to quickly run commands without seeing the output, you can omit this. If you're running several commands, you could place the PAUSE command in between them.

Writing a More Complex Batch File

It's fundamentally simple to create a batch file. The only thing you need to change is what you type into Notepad. To run several commands, you type each one on its own line and the batch file will run each one in order.

For example, let's say we want to write a batch file that runs several network diagnostic commands. We might want to run ipconfig /all to view network information, ping google.com to see if Google's servers are responding, and tracert google.com to run a traceroute to google.com and see if there are any problems on the way.

In the most basic form, we could simply place all those commands in a batch file, one after the other, like so:

ipconfig /all
ping google.com
tracert google.com
PAUSE

When we run this file, we'd just see the output of each command right after the other. But this isn't necessarily the ideal way to write a batch file.

For example, you might want to add comment lines. Any line that begins with a :: is a comment line and won't be executed. That makes them a useful way to explain what's happening in the file for anyone you might give it to — or for your future self, who might forget why you put a certain command in there.

You might also want to add the "ECHO OFF" command to the beginning of the file. This is typically added to the start of most batch files. When you do this, the commands themselves won't be printed to the Command Prompt, but the results will be. For example, you'll see the network connection details but not the "ipconfig /all" line. Most people don't care to see the commands, so this can clean up the output.

So here's what that might look like:

:: This batch file checks for network connection problems.
ECHO OFF
:: View network connection details
ipconfig /all
:: Check if Google.com is reachable
ping google.com
:: Run a traceroute to check the route to Google.com
tracert google.com
PAUSE

There are other directions you could go with a batch file like this. For example, you might want to have your batch script run the above commands and then dump the output to a text file you can view later. To do so, you'd use the >> operator after each command to append its output to the text file. As we're going to read the output from the text file anyway, we can omit the PAUSE command.

:: This batch file checks for network connection problems
:: and saves the output to a .txt file.
ECHO OFF
:: View network connection details
ipconfig /all >> results.txt
:: Check if Google.com is reachable
ping google.com >> results.txt
:: Run a traceroute to check the route to Google.com
tracert google.com >> results.txt

After you run the above script, you'd find a file named results.txt in the same folder as the batch file with the output of the commands. The Command Prompt window will automatically close once the batch file is done running.

The example we're using above relies on actually printing information to the Command Prompt so the user can read it. However, many batch files are designed to be run non-interactively. For example, you could have a batch file that deletes multiple files or directories whenever you double-click it. You'd just need to use the del command to delete files or the deltree command to delete directories. Remember, you're just using the same commands you'd run in a Command Prompt window.

Fundamentally, that's the point of most batch files — just running a few commands one after another. However, batch files can actually be significantly more complex than this. For example, you can use "IF" statements along with the "GOTO" command to check the value of something and then skip to different lines depending on the result. This is more like writing an actual small program than a quick and dirty script. That's one reason why .bat files are sometimes called "batch programs." If you want to do something more complex, you'll find plenty of guides to doing specific things with batch programming online. But now, you know the basics of how to throw a simple one together.

ncG1vNJzZmivp6x7qbvWraagnZWge6S7zGhpb2thbIRwtM6wZK2nXay%2FqsDEZphmmpGpsKl50pypoqikYryvedaipZ2np6h8