Software & OS

What Is a .bat File? Batch Scripts Explained With Examples

A .bat file is a plain-text file of Windows commands, called a batch file or batch script, that the Windows Command shell (cmd.exe) runs one line at a time. Double-click it or type its name in Command Prompt and every command inside runs in order, so you can repeat a task without typing it again.

The .bat ending is the only thing that makes it a batch file. There is no special format: open any .bat file in Notepad and you will see ordinary text.

What a .bat file is for

The Command shell was the first shell built into Windows, and Microsoft's documentation says it was used to automate routine tasks such as user account management or nightly backups with batch (.bat) files. Typical uses today:

  • Copying or backing up a set of files with one click.
  • Starting several programs, or a program with fixed options, in one step.
  • Running the same series of commands on many files with a loop.
  • Setting environment variables before launching a tool.
  • Gluing together installers or build steps that other programs call.

Anything you can type at the command prompt can go in a batch file. Microsoft's reference states that scripts accept all commands that are available at the command line.

A first .bat file

Open Notepad, paste the lines below, and save the file as hello.bat. In the Save As dialog, set "Save as type" to All files, or Notepad will add .txt to the name.

@echo off
echo Hello from a batch file.
echo.
echo Today's date is %DATE%.
pause

Double-click hello.bat. A Command Prompt window opens and runs the lines in order. Example output (your date format depends on your regional settings):

Hello from a batch file.

Today's date is 11/09/2026.
Press any key to continue . . .

What each line does:

  • @echo off stops the commands themselves from being printed on screen. Per Microsoft, putting echo off at the start of the file prevents echoing all commands, and the @ in front hides the echo off line too.
  • echo ... prints a message.
  • echo. prints a blank line. Do not put a space before the period, or the period is printed instead.
  • %DATE% is an environment variable. Putting a name between percent signs makes cmd replace it with its value when the script runs.
  • pause suspends the script and shows Press any key to continue . . ., which keeps the window open so you can read the output.

How to run a .bat file

  1. Double-click it in File Explorer. The window closes when the script ends, so end with pause if you want to read the output.
  2. Type its name in Command Prompt. Change to its folder first, then type the name:
    cd C:\scripts
    hello.bat
  3. Run it from another script with call. Microsoft describes call as calling one batch program from another without stopping the parent. For example, call checknew %1 %2 runs checknew.bat and passes it the parent's first two arguments.
  4. Run it through cmd explicitly: cmd /c hello.bat carries out the command and then exits the command processor. cmd /k hello.bat carries it out and keeps the command processor running, so the window stays open.

To edit a batch file, open it in Notepad, for example with notepad hello.bat. Double-clicking runs it instead of opening it.

The building blocks of a batch script

Variables

Create one with set and read it back with percent signs:

set include=c:\directory
dir %include%

Everything after the equals sign becomes the value, including spaces. set /p name=Your name: stores a line typed by the user, and set /a total=2+3 evaluates an arithmetic expression.

Arguments

Values typed after the script name arrive as %1, %2 and so on, with %0 being the script itself. %* stands for all arguments. Adding a tilde, as in %~1, removes surrounding quotation marks, and %~nx1 gives only the file name and extension of the first argument.

Conditions

The if command tests a file, a string or the exit code of the previous program:

if not exist product.dat echo Cannot find data file

Microsoft's documentation notes that an else clause must be on the same line as the command after the if. With parentheses, write it as ) ELSE ( on one line, as in the example below. For exit codes, if errorlevel 1 is true when the previous program returned an exit code of 1 or higher.

Loops

The for command repeats a command for each item. The loop variable is written differently in a file than at the prompt. Microsoft's reference says to use a single percent sign at the command prompt and double percent signs within a batch file:

rem At the command prompt:
for %f in (*.txt) do echo %f

rem Inside a .bat file:
for %%f in (*.txt) do echo %%f

If you paste a prompt-style loop into a batch file with a single percent sign, it does not work. Use %% in the file.

Comments and labels

rem starts a comment line. A word that begins with a colon, such as :end, is a label that goto end or call :end can jump to.

A practical example: back up a file

This script takes a file name as an argument and copies it to a .bak file next to the original. It checks that an argument was given and that the file exists.

@echo off
rem backup.bat - copy a file to filename.bak
if "%~1"=="" (
  echo Usage: backup.bat filename
) else (
  if exist "%~1" (
    copy "%~1" "%~1.bak"
  ) else (
    echo File not found: %~1
  )
)
pause

Run it as backup.bat notes.txt. Example output when the file exists:

        1 file(s) copied.
Press any key to continue . . .

Example output when it does not:

File not found: notes.txt
Press any key to continue . . .

The quotation marks around %~1 matter. Without them, a file name containing spaces would be split into several arguments.

Special characters that trip people up

  • &, |, <, >, ^ and parentheses are special to the command shell. To print one with echo, put a caret in front of it: echo Fish ^& chips. To print a caret, type two.
  • An empty variable after echo prints ECHO is off. Write echo:%var% to get a blank line instead.
  • Exclamation marks: Microsoft's echo page shows "Hello World^!" or Hello World^^! for printing ! in batch scripts.
  • Batch variable names with for are case sensitive, so %%a and %%A are different variables.

Are .bat files safe to run?

A batch file is only text, but it runs with the same permissions as the account that starts it. A script can delete files, change settings, or download and start other programs, depending on what its commands say. Treat a .bat file like any other program from the internet: open it in Notepad first, and if you do not understand a line, do not run it. A file downloaded from an unknown source that asks you to run it as administrator deserves extra suspicion.

Batch files or PowerShell?

PowerShell was designed to extend the capabilities of the Command shell. It runs cmdlets in addition to Windows commands, while the Command shell can run only Windows commands. Microsoft's current guidance is to use PowerShell rather than Windows Commands or Windows Script Host for Windows automation. Batch files remain common because they need no setup and every Windows installation can run them. For a short sequence of commands they are quick to write. For anything with structured data, error handling or many branches, PowerShell is the better fit.

If your .bat file does not work

  • It opens in Notepad, or is named .bat.txt. The extension is wrong. Turn on file name extensions in File Explorer and rename the file so it ends in exactly .bat.
  • 'xyz' is not recognized as an internal or external command, operable program or batch file. The command name is misspelled, or the program is not in a folder listed in your PATH variable. Use the full path to the program, in quotation marks if it contains spaces.
  • The window flashes and disappears. Add pause as the last line, or run the file from an open Command Prompt window.
  • A for loop is ignored or errors. Check that you used %%, not %, before the loop variable.
  • Everything is printed, including the commands. Add @echo off as the first line.

Frequently asked questions

Is a .bat file a virus?
No. A .bat file is just a text file of Windows commands, and it is harmless by itself. But it runs whatever commands it contains with your permissions, so a batch file from an unknown source can delete files or change settings. Open it in Notepad and read it before you run it.
What is the difference between a .bat file and a .cmd file?
Both are batch programs run by the Windows command interpreter, cmd.exe, and Microsoft's documentation for the call command lists both .bat and .cmd as valid batch program extensions. Most simple scripts work the same under either name.
Can I make a .bat file myself?
Yes. Open Notepad, type your commands one per line, and choose File, Save As. Set the file type to All files and give the name an ending of .bat, for example backup.bat. Saving it as backup.bat.txt is the usual mistake, so check that the extension is really .bat.
Why does the window close before I can read the output?
Double-clicking a batch file opens a Command Prompt window that closes when the script ends. Add the pause command as the last line and Windows will display 'Press any key to continue . . .' and wait. You can also run the file from a Command Prompt window that you opened yourself, which stays open.
Should I use PowerShell instead?
For new automation, Microsoft recommends PowerShell over Windows Commands and batch files. Batch files are still fine for short tasks, and they run on any Windows machine without extra setup.

Sources