Advertisement

Windows scripting reference

How to Create a Batch Script

This guide shows how to create, save, edit, and run a Windows batch script.

You can use batch scripts for simple automation, software deployment wrappers, troubleshooting commands, or repeatable Command Prompt tasks.

What is a batch script?

A batch script is a plain text file that runs Command Prompt commands in order. It uses the .bat file extension. Batch scripts are useful when you want to repeat the same commands without typing them manually each time.

File extension.bat
Default shellCommand Prompt
Best forSimple command wrappers, software install commands, file operations, and repeatable admin tasks.
Skill levelBeginner friendly when commands are copied carefully and tested first.

Step-by-step: create a batch script

Use these steps to create a batch script from scratch.

Create the script file

01
Create a working folder, for example C:\Scripts\MyTask.
02
Open Notepad.
03
Paste your Command Prompt commands into Notepad.
04
Click FileSave As.
05
Set Save as type to All Files.
06
Save the file with a .bat extension, for example run-task.bat.
07
Open Command Prompt in the script folder.
08
Run the batch file and verify the result.

Basic batch script template

This template shows a safe structure for a simple batch file: hide repeated command output, use the script folder as the working folder, run commands, and return the last exit code.

@echo off
cd /d "%~dp0"

echo Task started

REM Add your commands below this line

exit /b %errorlevel%

How to run a batch script

Open Command Prompt, move to the folder that contains the batch file, then run it by typing the file name.

cd /d "C:\Scripts\MyTask"
run-task.bat

Useful examples

Print a messageecho Hello
Change foldercd /d "C:\Scripts"
Run an installersetup.exe /quiet
Return an exit codeexit /b %errorlevel%

Beginner checklist

Correct extensionThe file ends with .bat, not .txt.
Plain textThe script was saved from Notepad or another plain text editor.
Working folderThe script is run from the folder where related files are stored.
PermissionsAdmin tasks are tested from an elevated Command Prompt.
TestingThe script is tested on a safe machine before production use.
Advertisement