Guide

Windows batch script: how to write and run a .bat file

A Windows batch script is a plain text file of Command Prompt commands, saved with a .bat or .cmd extension. Double-click it, or type its name in Command Prompt, and Windows runs the commands one after another, the same as if you typed them yourself.

Below: how to write and run your first batch file, the commands most scripts use, a working example, and how to run a script on another PC, or on many, without walking over to each one.

  • Write and run a .bat file
  • The commands you'll use most
  • Run it on remote PCs

Updated · Works on Windows 10 and 11

How to create and run a batch file

  1. Step 1: Open Notepad

    Click Start, type Notepad and open it. Any plain text editor works; a word processor doesn't.

  2. Step 2: Type the commands

    One command per line, for example:

    @echo off
    echo Hello from %COMPUTERNAME%
    ipconfig | findstr IPv4
    pause
  3. Step 3: Save it as a .bat file

    Choose File, Save as, set Save as type to All files and name it something like hello.bat. Without All files, Notepad saves hello.bat.txt, which won't run.

  4. Step 4: Run it

    Double-click the file, or open Command Prompt, go to its folder with cd and type its name. If a command needs administrator rights, right-click the file and choose Run as administrator.

.bat and .cmd behave the same for nearly every script; .cmd is the newer name. To run a script at a set time, add it to Task Scheduler (Start, type Task Scheduler, then Create Basic Task).

Batch commands you'll use most

Common Windows batch script commands
CommandWhat it does
@echo offDon't print each command before running it. Almost every script starts with it.
rem or ::A comment, for notes to yourself.
echo HelloPrints a line.
set NAME=valueMakes a variable; use it as %NAME%.
if exist file (...)Runs something only when a file or folder is there.
for %%f in (*.log) do ...Repeats a command for each file. In a script it's %%f; typed at the prompt it's %f.
call other.batRuns another script and comes back.
pauseWaits for a key, so the window doesn't close before you read it.
exit /b 0Ends the script with an exit code (0 means it worked).

Any program you can run in Command Prompt works in a script too: robocopy, ipconfig, tasklist, taskkill, shutdown and the rest. Type a command followed by /? to see its options.

Example: copy a folder to a backup drive

@echo off
:: Copy Documents to drive E, keeping a log
set SRC=%USERPROFILE%\Documents
set DST=E:\Backup\Documents
if not exist E:\ (
  echo The backup drive isn't plugged in.
  exit /b 1
)
robocopy "%SRC%" "%DST%" /E /R:1 /W:1 /LOG:"%DST%-log.txt"
if %ERRORLEVEL% GEQ 8 (echo Some files failed to copy.) else (echo Backup done.)

robocopy exit codes under 8 mean it worked, which is why the last line checks for 8 or more. Quotes around the paths keep folder names with spaces working.

Run a batch script on a remote PC

On your own network, PowerShell can run a script on other PCs; see remote PowerShell. For PCs anywhere, RemoteAccessApp runs commands without a remote session:

  • On the Devices page, select one PC or several, right-click and choose Run a command.
  • Pick Command Prompt and paste the script's lines. They're typed into Command Prompt one by one, as if from the keyboard, so write %f rather than %%f in a for loop and leave out exit.
  • For a longer script, send the .bat file to the PCs first (Send to several PCs on the File transfer page), then run it by its full path.
  • It runs on every selected PC at once, as the person signed in there, and each PC shows its output and exit code.
  • Unless unattended access lets you in, the person at each PC sees the exact commands and chooses whether they run.

Run a command without a remote session has the steps, and run a script on multiple PCs covers saved scripts and schedules, which are part of Pro.

Want Linux commands instead?

Windows can run bash scripts too, through the Windows Subsystem for Linux. See how to run Linux commands on Windows.

Questions, answered

Related