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
Step 1: Open Notepad
Click Start, type Notepad and open it. Any plain text editor works; a word processor doesn't.
Step 2: Type the commands
One command per line, for example:
@echo off echo Hello from %COMPUTERNAME% ipconfig | findstr IPv4 pause
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.
Step 4: Run it
Double-click the file, or open Command Prompt, go to its folder with
cdand 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
| Command | What it does |
|---|---|
@echo off | Don't print each command before running it. Almost every script starts with it. |
rem or :: | A comment, for notes to yourself. |
echo Hello | Prints a line. |
set NAME=value | Makes 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.bat | Runs another script and comes back. |
pause | Waits for a key, so the window doesn't close before you read it. |
exit /b 0 | Ends 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?
Questions, answered
Related
- Run a script on many PCsOne command or saved script on many PCs, now or on a schedule, with each PC's output and the person there kept informed.Read the guide
- Run a command without a sessionRun one command or VBScript on your PCs without a live session: straight away under unattended access, otherwise after the person there allows it.Read the guide
- Remote PowerShellEnable-PSRemoting, Enter-PSSession and Invoke-Command for PCs with no domain, enabling Remote Desktop from PowerShell or cmd, and a way with no setup.Read the guide
- Linux commands on WindowsInstall the Windows Subsystem for Linux, run Linux commands and bash scripts on Windows, and the Command Prompt names for common Linux commands.Read the guide
- Task Manager shortcutCtrl+Shift+Esc and five other ways to open Task Manager, ending a frozen program, and opening it on a PC you use remotely.Read the guide
- Manage multiple computersOne list for all your PCs: who's online, one-click sessions, commands on many PCs, Windows tools, tags and scheduled jobs.Read the guide
