What a batch file is and why you might need one

A batch file is a text file that contains a list of commands Windows runs one after another. When you double-click the file, Windows executes every command in it without you typing each one separately. Batch files end with the .bat extension and are useful for automating repetitive tasks — like backing up folders, restarting services, or running network diagnostics on your LAN.

On a wired network, you might use a batch file to ping multiple devices at once, map network drives automatically when you log in, or run network troubleshooting tools without opening the command prompt manually each time. The file itself is just plain text, so you can create one with Notepad or any text editor.

Key Takeaways

  • Open Notepad, type your commands (one per line), then save the file with a .bat extension instead of .txt.
  • Each line in the batch file must be a valid Windows command — common ones include ping, ipconfig, net use, and echo.
  • Test your batch file by right-clicking it and choosing Run as administrator if the commands need elevated permissions.
  • You can edit a batch file later by right-clicking it, choosing Edit, making changes in Notepad, and saving.

Creating a batch file in Notepad

Open Notepad on your Windows computer. You can find it by pressing the Windows key, typing "Notepad", and pressing Enter. A blank text window will open.

Type the commands you want to run, one command per line. For example, if you want to check the IP configuration of your computer and then ping a device on your network, you would type:

ipconfig ping 192.168.1.1

Each line executes in order. The batch file will wait for one command to finish before moving to the next, so if a command takes time (like a ping with multiple packets), the file will pause until it completes.

Saving the file with the correct extension

Go to File > Save As. A dialog box will open. In the "File name" field, type a name for your batch file followed by .bat — for example, network-check.bat. Do not use .txt, even if Notepad suggests it.

In the "Save as type" dropdown, select "All Files" instead of "Text Documents". This prevents Notepad from automatically adding .txt to your filename. Choose where you want to save it — your Desktop or a folder in Documents works fine — then click Save.

After you save, you will see a file icon with the name you chose. If the icon looks like a text document instead of a command prompt window, you saved it as .txt by mistake. Right-click the file, choose Rename, and change the extension from .txt to .bat.

Running your batch file

Double-click the batch file to run it. A command prompt window will open, execute each command in order, and display the results. The window may close automatically when finished, or it may stay open so you can read the output.

If your batch file needs to run commands that require administrator permissions — such as restarting a network service or modifying network settings — right-click the batch file instead and choose "Run as administrator". Windows will ask for permission, and then the file will run with elevated privileges.

If nothing happens when you double-click, check that the file extension is actually .bat. You can verify this by right-clicking the file, choosing Properties, and looking at the "Type of file" line. It should say "Batch File" or "MS-DOS Batch File", not "Text Document".

Common commands for network tasks

ipconfig displays your computer's IP address, subnet mask, and gateway. Use ipconfig /all to see more details including MAC address and DNS servers. This is useful for verifying your network configuration before troubleshooting.

ping followed by an IP address or hostname tests whether a device on your network is reachable. For example, ping 192.168.1.1 sends packets to your router. Add -t at the end to ping continuously until you stop it manually — ping 192.168.1.1 -t.

net use maps a network drive or shared folder to a letter on your computer. For example, net use Z: \\server\share connects to a shared folder called "share" on a computer named "server" and assigns it the letter Z. You can then access it like any other drive.

echo prints text to the screen. Use it to add labels or instructions — for example, echo Checking network connectivity... will display that message before the next command runs. This helps you understand what the batch file is doing.

Editing and troubleshooting your batch file

If you need to change the commands in your batch file, right-click it and choose "Edit". Notepad will open with your commands. Make your changes, save the file (Ctrl+S), and close Notepad. The next time you run the batch file, it will use the updated commands.

If a command does not work, check the spelling and syntax. Windows command prompt is strict — a typo or missing space will cause an error. You can test individual commands by opening Command Prompt (press Windows key, type "cmd", press Enter) and typing the command to see if it works before adding it to your batch file.

If a batch file window closes too quickly to read the output, add pause as the last line. This command pauses the window and displays "Press any key to continue", giving you time to read the results before it closes.

Frequently Asked Questions

Can I run a batch file from a network location?

Yes, you can store a batch file on a shared network folder and run it from there, but Windows may block it for security reasons. If you get a security warning, move the file to your local computer, run it, and then move it back if needed. Alternatively, right-click the file and check the Properties — if there is an "Unblock" button at the bottom, click it before running.

What if I want the batch file to pause between commands?

Add timeout /t 5 on its own line to pause for 5 seconds (replace 5 with any number of seconds you want). This gives you time to read output before the next command runs. You can also use pause to wait for you to press a key.

Can I make a batch file run automatically when I log in?

Yes. Save your batch file, then press Windows key + R, type shell:startup, and press Enter. A folder will open. Copy your batch file into this folder, and it will run automatically the next time you log in to Windows.

How do I stop a batch file that is running?

Click the command prompt window and press Ctrl+C. This interrupts the current command. If the window is frozen, close it by clicking the X button in the top right corner, though this may not stop the command itself if it is running in the background.

What is the difference between .bat and .cmd files?

.bat and .cmd are both batch files and work almost identically on modern Windows. .bat is the older format and .cmd is newer, but both will run your commands. For simplicity, use .bat unless you have a specific reason to use .cmd.