How to Create and Run a Batch File in Windows 10 and 11

To create a batch file, type Command Prompt commands into Notepad, save the file with a .bat extension, and double-click it to run it on Windows 10 or 11.

Advertisement

This guide covers the basic file, every way to run it (including as administrator), safe examples, variables, input, arguments, scheduling with schtasks, startup, and fixes for the usual errors.

How to Create a Basic Batch File

A batch file is plain text, so Notepad is all you need. These steps create hello.bat on your desktop and run it.

  1. Type notepad in the search box on the taskbar and select Notepad in the results.
  2. Type these three lines exactly: @echo off, then echo Hello from my first batch file, then pause.
  3. Save the file to your Desktop as hello.txt.
  4. Open File Explorer and turn on View > Show > File name extensions so you can see the .txt ending.
  5. Rename hello.txt to hello.bat on your desktop and confirm the change if Windows asks.
  6. Double-click hello.bat. A console window shows Hello from my first batch file and Press any key to continue . . .
  7. Press any key to close the window.

@echo off hides the commands themselves, echo prints text, and pause keeps the window open until you press a key. Without pause, a double-clicked batch file closes as soon as its last line finishes.

What Is a Batch File?

A batch file is a text file of Command Prompt commands that cmd.exe runs one line at a time, top to bottom. Microsoft lists the .bat extension as a "PC batch file".

Property What it means for you
File type Plain text. Any text editor can create or edit it; Notepad ships with Windows 10 and 11.
Extensions .bat or .cmd. The call command accepts either, and both appear in the default PATHEXT list (.COM;.EXE;.BAT;.CMD;...).
What runs it The Command Prompt interpreter, cmd.exe. Commands such as echo, set, if, for and goto are built into it.
Order of execution Line by line, top to bottom, unless goto or call sends processing to a label.
Permissions The file runs with your account's rights. It gets administrator rights only when you run it elevated.
Stopping it Press Ctrl + C, then Y at the Terminate batch job (Y/N)? prompt.

Before You Start: Important Notes About Batch Files

Item What you need to know
Editor Notepad is enough. Save the file as plain text, not in a word processor format such as .docx.
Visible extensions Turn on file name extensions first, or Windows may hide a .txt ending and leave you with hello.bat.txt.
Admin rights Not needed for the examples in this guide. Elevate only when a command genuinely needs it.
Command extensions Enabled by default in Windows 10 and 11. set /p, if /i, for /l and goto :eof depend on them.
Spaces in paths Wrap any path that contains a space in double quotes, for example "C:\Program Files".
Destructive commands Commands such as del, format and reg act immediately with no undo. Read any batch file you did not write before you run it.

How to Show File Extensions in Windows

Hidden extensions are the most common reason a new batch file refuses to run: it is really hello.bat.txt and opens in Notepad. Microsoft gives the same route for Windows 11 and Windows 10.

  1. Type file explorer in the search box on the taskbar and select File Explorer.
  2. Select View > Show > File name extensions.
  3. Look at your batch file: it should now read hello.bat, not hello.bat.txt.
  4. If you see .bat.txt, rename the file and delete the .txt ending.

Changing an extension only renames the file; it does not convert anything. That is exactly why renaming hello.txt to hello.bat works: the content was already plain text.

File Explorer View menu with File name extensions option highlighted
Selecting Show, then File name extensions, reveals the .bat ending Windows normally hides. (Image: Microsoft)

How to Run a Batch File

Your situation Run it this way Why
You just want it to run Double-click the .bat file in File Explorer or on the desktop. Fastest route. Add pause at the end or the window closes when the script finishes.
You want to read the output or errors Open Command Prompt, type the full path in quotes, for example "%USERPROFILE%\Desktop\hello.bat", and press Enter. The window stays open after the script ends, so error messages stay on screen.
You work in Windows Terminal or PowerShell cd to the folder, then type .\hello.bat and press Enter. PowerShell does not run files from the current folder unless you prefix them with .\.
The script changes system settings Run it as administrator (next section). Commands that need elevation fail with access errors in a normal window.
It must run on a schedule or at sign-in Use schtasks or the Startup folder (sections below). No need to remember to launch it.
You want to start it from another batch file call other.bat call runs the second file and then returns to the first one.

How to Run a Batch File as Administrator

Elevate only scripts you wrote or have read line by line. An elevated batch file can change anything on the PC.

Advertisement
  1. Right-click the .bat file and select Run as administrator. If your account is not an administrator, Windows asks for an administrator user name and password.
  2. Select Yes at the User Account Control prompt.
  3. To keep the output on screen instead, type command prompt in the taskbar search box, select Run as administrator, then Yes.
  4. On Windows 11 you can also press Windows key + X and select Windows Terminal (Admin).
  5. In the elevated window, type the full path to the batch file in quotes and press Enter. In a PowerShell tab, cd to the folder and type .\hello.bat.
  6. Check the title bar: an elevated window shows Administrator.
Command Prompt and PowerShell tabs, one labeled Administrator in the title
An elevated window's title bar reads Administrator, unlike the plain PowerShell tab beside it. (Image: Microsoft)

Useful Batch File Examples

Each example below is safe to run: it opens, copies or displays things and deletes nothing. Save each one as its own .bat file.

@echo off
title Start my workday
rem Open apps, a folder and a website in one go
start notepad
start "" "%USERPROFILE%\Documents"
start "" "https://www.bing.com"
start "" "https://learn.microsoft.com"

Open several programs at once and open websites in your default browser. start launches each item without waiting for it to close. The empty "" matters: start treats the first quoted string as the window title, so without it a quoted path or URL becomes the title and nothing opens. A folder path opens in File Explorer, and a URL opens in the default browser.

You should see: Notepad, your Documents folder and two browser tabs open, and the batch window closes by itself.

Create a Simple Backup Script

@echo off
rem Copy Documents to a backup folder. Change D:\Backup to a drive you own.
robocopy "%USERPROFILE%\Documents" "D:\Backup\Documents" /e /log:"%USERPROFILE%\Desktop\backup.log"
if %errorlevel% GEQ 8 (
  echo Some files failed to copy. Open backup.log on your desktop.
) else (
  echo Backup finished.
)
pause

robocopy ... /e copies every file and subfolder, including empty folders, and never deletes anything at the source. /log: writes a report you can read later. Robocopy exit codes of 8 or higher mean at least one file failed, which is what the if %errorlevel% GEQ 8 line checks. Avoid /mir in a first script: it mirrors the source and removes extra files from the destination.

Advertisement

You should see: Backup finished. on screen, a D:\Backup\Documents folder with your files, and a backup.log file on the desktop.

Display System Information

@echo off
title System summary
echo Signed in as %USERNAME%
ver
echo.
systeminfo /fo list
pause

%USERNAME% expands to your account name, ver prints the Windows version number, echo. prints a blank line, and systeminfo /fo list lists OS, memory, network card and hardware details one per line.

You should see: Your user name, the Windows version number, then a long list of system details, with the window held open by pause.

Common Batch File Commands

Command What it does Example
@ Hides the echo of a single line. @echo off
echo Prints text; echo off hides the commands that follow. echo. prints a blank line. echo Backup started
rem Adds a comment that is never run or shown. rem Runs every Friday
pause Stops and shows Press any key to continue . . . pause
set Creates or changes a variable; /p asks for input, /a does arithmetic. set /a total=5+3
if Runs a command only when a condition is true, with an optional else. if exist notes.txt echo Found it
for Repeats a command for each file, string or number. Use %% in a batch file, % at the prompt. for %%f in (*.txt) do echo %%f
goto Jumps to a label that starts with a colon; goto :eof ends the script. goto end
call Runs another batch file or a label, then comes back. call backup.bat
start Opens a program, folder or URL in a separate process. start "" "https://www.bing.com"
cmd Starts a new interpreter; /c runs a command and exits, /k keeps the window open. cmd /k hello.bat
title Sets the text in the console window's title bar. title Nightly backup
timeout Waits a number of seconds; /nobreak ignores key presses. timeout /t 10
cd /d Changes the current folder, including the drive. cd /d "%~dp0"

How to Edit a Batch File

Double-clicking a batch file runs it, so open it in an editor instead whenever you want to read or change it.

Advertisement
  1. Right-click the .bat file and select Open with > Choose another app.
  2. Select Notepad from the list and open the file.
  3. Make your changes and save the file. Notepad keeps the .bat name.
  4. Run the file again from a Command Prompt window so any new error stays on screen.

How to Add Comments to a Batch File

@echo off
rem Purpose: open the tools I use every morning
rem Author: your name, last changed 2026-09-19
:: Lines that start with a colon are labels, so :: also works as a comment
start notepad

rem lines are skipped and never shown while echo is off. A line that begins with a colon is treated as a label and ignored, which is why :: is a common shorthand. You cannot put a pipe | or redirection < > character inside a rem comment.

You should see: Only Notepad opens; none of the comment text appears in the window.

How to Use Variables in Batch Files

@echo off
set folder=%USERPROFILE%\Documents
set /a files=0
for %%f in ("%folder%\*.txt") do set /a files+=1
echo %folder% holds %files% text files.
pause

set name=value creates a variable and %name% reads it back. Everything after the equal sign becomes the value, so do not add spaces around =. set /a treats the value as a number, and += adds to it. Inside a for loop in a batch file, the loop variable uses two percent signs (%%f).

You should see: A line such as C:\Users\Alex\Documents holds 4 text files.

How to Ask for User Input

@echo off
set /p name=What is your name? 
echo Hello, %name%.
set /p answer=Open your Documents folder (y/n)? 
if /i "%answer%"=="y" (
  start "" "%USERPROFILE%\Documents"
) else (
  echo Skipped.
)
pause

set /p shows the prompt text and stores whatever the user types. if /i compares the answer without caring about case, so Y and y both match. Quotes around both sides keep the comparison working when the user just presses Enter.

You should see: The script greets you by name, then either opens Documents or prints Skipped.

How to Schedule a Batch File to Run Automatically

schtasks creates the same kind of task you would build in the Task Scheduler app, from one line. Put your script in a folder without spaces, such as C:\Scripts, and use its full path.

schtasks /create /tn "Daily Backup" /tr C:\Scripts\backup.bat /sc daily /st 18:00

/tn names the task, /tr is the full path to the batch file, /sc daily sets the schedule and /st 18:00 is the start time in 24-hour format. Swap /sc daily for /sc weekly, /sc onlogon (whenever any user signs in) or /sc onstart (every time the PC starts). Add /rl highest if the script needs administrator rights; the default is limited. Test it at once with schtasks /run /tn "Daily Backup", and remove it later with schtasks /delete /tn "Daily Backup".

You should see: No error message, and schtasks /query /tn "Daily Backup" lists the task with its next run time. It also appears under the Task Scheduler Library folder in the Task Scheduler app.

How to Make a Batch File Run at Startup

The Startup folder runs a shortcut each time you sign in. For a script that must run before anyone signs in, use schtasks ... /sc onstart from the previous section instead.

  1. Right-click Start and select Run.
  2. Type shell:startup and press Enter. For every user on the PC, type shell:common startup instead.
  3. Create a shortcut to the batch file (see the next section), then drag that shortcut into the Startup folder. Keep the original .bat where it is.
  4. Sign out and back in to confirm the script runs.

The current-user folder is %userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. To stop the script running at sign-in, delete the shortcut from that folder; the batch file itself stays untouched.

How to Create a Desktop Shortcut for a Batch File

  1. Open the folder that holds your batch file in File Explorer.
  2. On Windows 11, right-click the .bat file and select Show more options > Send to > Desktop (create shortcut).
  3. On Windows 10, right-click the .bat file and select Send to > Desktop (create shortcut).
  4. Double-click the new desktop shortcut to run the script.

A shortcut keeps your script in a safe folder such as C:\Scripts while you launch it from the desktop. Moving or renaming the original file breaks the shortcut.

How to Pass Arguments to a Batch File

@echo off
rem Save as greet.bat, then run:  greet.bat Alex "New York"
if "%~1"=="" (
  echo Usage: greet.bat Name City
  goto :eof
)
echo Hello, %~1 from %~2.
echo All arguments: %*
echo This script lives in %~dp0

%1 to %9 hold the words typed after the file name, and %* holds all of them. The ~ in %~1 strips surrounding quotes, so "New York" prints as New York. %0 is the batch file itself, and %~dp0 expands it to the drive and folder the script lives in. Use shift to read more than nine arguments.

You should see: greet.bat Alex "New York" prints Hello, Alex from New York. Running it with no arguments prints the usage line and stops.

How to Check Your Batch File Worked

  1. Open Command Prompt and run the script by its full path in quotes, so the window stays open after the last line.
  2. Read every line of output. A message such as 'xyz' is not recognized as an internal or external command points to the exact line that failed.
  3. Type echo %errorlevel% and press Enter. 0 means the last command reported success; any other number means it reported a problem (for robocopy, 8 or higher).
  4. For a scheduled script, run schtasks /query /tn "Daily Backup" /fo LIST /v and check the schedule, next run time and last result.
  5. Run schtasks /run /tn "Daily Backup" to trigger it immediately, then confirm the result it produces, such as new files or a log.

How to Troubleshoot Batch Files

The window opens and closes immediately

A double-clicked batch file closes as soon as it finishes or hits an error.

  1. Add pause as the last line to hold the window open.
  2. Better, run the file from an open Command Prompt window so the error stays visible.
  3. Fix the line named in the error, then remove pause if you no longer need it.

The file opens in Notepad instead of running

The file is really name.bat.txt, because Windows hid the .txt ending.

  1. In File Explorer, select View > Show > File name extensions.
  2. Rename the file so it ends in .bat only.
  3. Double-click it again.

Paths with spaces do not work

Command Prompt splits unquoted text at spaces, so C:\Program Files becomes two words.

  1. Wrap every path that contains a space in double quotes.
  2. With start, add an empty title first: start "" "C:\My Folder\report.txt".
  3. Quote variables that may hold spaces, for example "%folder%".

Commands fail because of the wrong folder

Relative file names resolve against the current folder, which is not always the folder that holds the script.

  1. Add cd /d "%~dp0" near the top to switch to the script's own folder.
  2. Or use full paths for every file the script touches.
  3. Use a full path for the script itself in schtasks /tr; without one, schtasks looks in the System32 folder.

The script needs administrator permission

Commands that change system settings return Access is denied in a normal window.

  1. Right-click the file and select Run as administrator, then Yes.
  2. For a scheduled task, recreate it with /rl highest.
  3. Keep the elevated part as small as possible; everyday scripts should not need it.

Special characters break the script

&, |, <, > and ^ are command characters to Command Prompt, not plain text.

  1. Put a caret before the character in echo text: echo Tom ^& Jerry.
  2. Type two carets ^^ to print one caret.
  3. Inside an if or for block in parentheses, escape parentheses in text as ^( and ^).

"ECHO is off." appears instead of a value

The variable after echo is empty or was never set.

  1. Check the variable name with set name at the prompt.
  2. Use echo:%name% to print a blank line instead of the message when the value is empty.
User Account Control prompt asking to allow changes, Yes or No
Select Yes here after choosing Run as administrator on a script that needs elevation. (Image: Microsoft)

Batch File Safety Tips

Tip Why it matters
Open unknown batch files in Notepad before running them Double-clicking runs every line immediately, with no preview and no undo.
Test risky lines with echo first Writing echo copy ... instead of copy ... shows what would happen without doing it.
Run without admin rights first An elevated script can change system files and settings; a normal one cannot.
Never run del, format or reg lines you do not understand They act at once. format erases a whole drive.
Keep scripts in one folder such as C:\Scripts Easier to back up, audit and point scheduled tasks at.
Do not hard-code passwords A batch file is plain text that anyone with access can read.

Should You Use .bat or .cmd?

Use .bat unless your team already standardises on .cmd. Both extensions run in the same interpreter, both are accepted by call, and both sit in the default PATHEXT list, where .BAT comes before .CMD. Microsoft's own list of file types names .bat as the PC batch file, so it is the extension readers and colleagues recognise. Every example in this guide behaves the same under either name.

Frequently Asked Questions

How do I create and run a batch file in Windows 11?

Type your commands in Notepad, save the file, and rename it so it ends in .bat with file name extensions visible. Then double-click the file, or run it by its full path in Command Prompt to keep the output on screen.

Why does my batch file close immediately?

A double-clicked batch file closes as soon as its last line runs or an error stops it. Add pause as the final line, or run the file from an open Command Prompt window so the output and any error message stay visible.

How do I run a batch file from PowerShell or Windows Terminal?

Change to the file's folder with cd, then type .\name.bat and press Enter. PowerShell does not run files from the current folder without the .\ prefix. Running it from Command Prompt instead needs only the file name or full path.

Is a batch file the same as a PowerShell script?

No. A batch file (.bat or .cmd) runs Command Prompt commands through cmd.exe, while a PowerShell script (.ps1) runs PowerShell commands. Microsoft points users who need more advanced scripting and automation towards PowerShell.

How do I stop a batch file that is running?

Click the console window and press Ctrl + C. Windows asks Terminate batch job (Y/N)?, and pressing Y ends the script and returns control to the prompt. Closing the console window also stops it.

Can a batch file run another batch file?

Yes. Use call other.bat inside the first file. call runs the second script and then returns to the next line of the first. Typing the second file's name without call hands control over and never comes back.

How do I hide the commands in a batch file window?

Put @echo off on the first line. echo off stops each command from being printed, and the @ hides the echo off line itself. Only the text you print with echo and any command output will appear.

How do I make a batch file wait before the next command?

Use timeout /t 10 to wait ten seconds, or timeout /t 10 /nobreak to ignore key presses during the wait. Use pause when the script should wait until someone presses a key.

Can I run a batch file automatically at a set time?

Yes. Run schtasks /create /tn "My Task" /tr C:\Scripts\task.bat /sc daily /st 09:00 to run it every day at 9:00. Use /sc onlogon for every sign-in, and test the task with schtasks /run /tn "My Task".

Where can I see a real-world batch file in use?

Many Windows fixes ship as short batch files that chain several commands. For an example, see how to remove OneDrive from Windows 10 with a batch file. Read any script like that in Notepad before you run it, and understand every line first.

Philip Celasco

Philip is a Texas-based technology writer and IT administrator at Techdows.com with more than 10 years of experience creating practical content for everyday users and professionals. He specializes in web browsers, particularly Chromium-based platforms such as Google Chrome, Microsoft Edge, Brave, and Opera. Through his work as an IT administrator, Philip has hands-on experience managing devices, configuring browser policies, troubleshooting software and network issues, and helping people resolve problems that affect productivity and security. His articles are based on practical testing and real-world technical experience. He covers browser settings, extensions, performance problems, privacy controls, security features, and Windows troubleshooting. Outside work, Philip enjoys the quieter side of life in Texas and stepping away from the screen when he can. He has two kids, two cats and loves to play golf with his mother during the weekends.

Leave a Reply

Your email address will not be published. Required fields are marked *