How to Make a Batch File in Windows (Step-by-Step Guide)

A batch file is a plain text file containing a sequence of commands that Windows executes automatically, one after another, as if you typed them into the Command Prompt yourself. They're one of the oldest and most practical tools in Windows — useful for automating repetitive tasks, running maintenance scripts, launching programs in sequence, or organizing file operations without touching any complex programming language.

What Is a Batch File, Really?

Batch files use the .bat (or sometimes .cmd) file extension. When you double-click one, Windows passes the contents to cmd.exe, the Command Prompt interpreter, which runs each line as a command.

The language inside is called batch scripting — a stripped-down scripting syntax that's been part of Windows since the DOS era. It's not Python or PowerShell; it's simpler, more limited, and requires no installation. Every Windows machine can run a batch file out of the box.

.bat vs .cmd — both work in modern Windows. The .cmd extension ties the file specifically to cmd.exe and handles some error conditions slightly differently. For most everyday use, .bat is the standard choice.

What You Need Before You Start

  • A Windows PC (any modern version: Windows 10 or 11 works identically for this)
  • A plain text editor — Notepad is already installed and works perfectly
  • No coding experience required for basic scripts

⚙️ One important rule: batch files must be plain text. Never write them in Microsoft Word or any rich-text editor. Those programs add invisible formatting that will break your script.

How to Create a Batch File: The Core Steps

Step 1 — Open Notepad

Press Win + R, type notepad, and hit Enter. Alternatively, right-click anywhere on your desktop, select New > Text Document, and open the file.

Step 2 — Write Your Commands

Type your commands exactly as you would in the Command Prompt. Each command goes on its own line.

A simple example that creates a folder and opens it:

@echo off mkdir C:MyNewFolder echo Folder created successfully. pause 

What each line does:

LinePurpose
@echo offStops the command text from printing to screen as it runs
mkdir C:MyNewFolderCreates a new folder at that path
echo Folder created successfully.Prints a message to the screen
pauseKeeps the window open so you can read the output

Step 3 — Save It as a .bat File

Go to File > Save As. In the "Save as type" dropdown, change it from Text Documents (*.txt) to All Files. Then name your file with the .bat extension — for example, myscript.bat — and save it wherever you like.

This step is where most beginners stumble. If you forget to change the file type dropdown, Windows saves it as myscript.bat.txt, which won't execute as a batch file.

Step 4 — Run the File

Double-click the file. A Command Prompt window opens and runs your commands in sequence.

🖱️ To run it with administrator privileges (required for system-level tasks like editing protected folders or modifying registry paths), right-click the file and select Run as administrator.

Useful Commands to Know

Once you understand the basic structure, these commands cover a wide range of practical tasks:

  • echo — prints text to the screen
  • pause — waits for a keypress before continuing
  • cls — clears the Command Prompt window
  • start — launches a program or opens a file
  • del — deletes a file
  • copy / xcopy / robocopy — copies files, with increasing levels of control
  • cd — changes the current directory
  • if / else — adds conditional logic
  • for — loops through a set of files or values
  • set — creates variables
  • rem — adds a comment (the line is ignored during execution)

Variables, Logic, and Loops

For anything beyond a simple sequence of commands, batch scripting supports basic logic:

Setting and using a variable:

set name=TechFAQs echo Hello, %name% 

A simple conditional:

if exist C:MyFile.txt (echo File found) else (echo File missing) 

Looping through files in a folder:

for %%f in (C:MyFolder*.txt) do echo %%f 

Note the double %% — that's required when using variables inside a loop within a batch file. At the Command Prompt directly, you'd use single %.

Where Batch Files Fit vs. Other Options

Batch scripting has real limits. It handles text processing clumsily, struggles with complex logic, and has no built-in error handling beyond basic if errorlevel checks. As tasks grow in complexity, many users migrate toward PowerShell, which runs on all modern Windows systems and is dramatically more capable — supporting objects, modules, remote execution, and structured error handling.

For cross-platform needs, Python scripts are another common step up. For purely system-level Windows automation with a GUI, Task Scheduler pairs well with batch files to run scripts on a schedule.

ToolBest ForComplexity Threshold
Batch (.bat)Simple task automation, quick scriptsLow
PowerShell (.ps1)System administration, complex logicMedium
Python (.py)Data processing, cross-platform tasksMedium–High

The Variables That Determine What You'll Actually Build

The right batch file for your situation depends on factors that vary considerably from user to user: whether you need it to run silently in the background or interact with you, whether it's touching protected system files (which requires elevated permissions), whether it needs to run on a schedule, and how comfortable you are reading command-line output when something goes wrong.

A simple one-liner that opens three programs at startup looks nothing like a script that backs up files, checks for errors, logs results, and sends an alert if something fails — even though both are technically "batch files." Your use case, your Windows environment, and how much you're willing to troubleshoot will shape what your script actually needs to include.