How to Run a Command in the Background in a Batch Script
By default, a batch script executes commands synchronously. This means that when it runs a command, it stops and waits for that command to finish before moving on to the next line. This is a problem when a script needs to launch a long-running task but also needs to continue with other work. The solution is to run the task asynchronously, or "in the background."
This guide will teach you how to use the powerful, built-in START command to launch a new process in the background. You will learn how to run a program without a new window, how to launch internal cmd commands, and the critical syntax rules you need to know to avoid common errors.
The Challenge: Synchronous vs. Asynchronous Execution
Consider this simple script:
@ECHO OFF
ECHO Starting a long 10-second ping...
PING -n 10 127.0.0.1 > NUL
ECHO The ping has finished.
The script will print the first message, wait the full 10 seconds for the PING command to complete, and only then will it print the final message. We need a way to launch the PING and have our script immediately continue.
The Core Command: START
The START.exe command is the standard Windows utility for starting a program or command in a new, separate process. Because it runs in a new process, the original batch script does not have to wait for it to finish.
Syntax: START "Window Title" [options] "command"
"Window Title": This is a quirky but required part of the syntax. TheSTARTcommand always interprets the first quoted string it sees as the title for the new window.[options]: Switches that control the behavior, like/B(background) or/LOW(priority)."command": The program or command you want to run.
Basic Example: A "Fire and Forget" Task
This script launches a long PING command but does not wait for it.
@ECHO OFF
ECHO --- Main Script ---
ECHO Launching a background PING process now...
START "My Background Ping" PING -n 10 127.0.0.1
ECHO The main script is continuing immediately without waiting.
ECHO The PING is running in its own window.
When you run this, a new command prompt window titled "My Background Ping" will appear and start its 10-second ping. At the same time, the original script window will immediately print its final messages.
Running a Command Prompt Command in the Background
The START command is designed to run executables (.exe). It cannot directly run internal cmd.exe commands like DIR, COPY, or DEL. To do this, you must explicitly tell START to launch a new CMD instance to run the command.
Syntax: START "Title" CMD /C "your command here"
CMD /C: This starts a new command processor, tells it to Carry out the command string, and then terminate.
For example:
REM This will run a recursive DIR command in a new window.
START "Background Directory Listing" CMD /C "DIR C:\ /S"
Running a Command Without a New Window (/B)
Sometimes, you want a task to run in the background with no visible window. This is perfect for non-interactive tasks like file copies or backups. The /B switch runs the application in the same console window, but asynchronously.
For example, this script starts a robocopy backup process that runs in the background without opening a new window.
@ECHO OFF
ECHO Starting a background Robocopy process...
ECHO This will run with no new window.
START "Title" /B robocopy "C:\MyData" "E:\Backups" /E
ECHO Main script is continuing...
The robocopy output will start to appear in your current command window, but the script will have already moved on.
Key START Parameters Explained
| Switch | Description |
|---|---|
/B | Starts an application without creating a Background window. |
/WAIT | Starts an application and waits for it to terminate. (This makes it synchronous, the opposite of our goal). |
/MIN | Starts the new window Minimized. |
/MAX | Starts the new window Maximized. |
/LOW, /HIGH, etc. | Starts the application with a specific priority class. |
Common Pitfalls and How to Solve Them
-
Forgetting the "Title": This is the #1 mistake.
STARTalways treats the first quoted string as a title.- WRONG:
START "C:\Program Files\MyApp\App.exe"(This will open a newcmdwindow with the titleC:\Program...) - RIGHT:
START "" "C:\Program Files\MyApp\App.exe"(Provide a blank title)
- WRONG:
-
Cannot Get the Exit Code: This is a critical limitation. Because the process is detached,
%ERRORLEVEL%in your main script will only reflect the success of theSTARTcommand itself (0), not the result of the background task.- Solution: There is no simple solution. For advanced scripts that need the exit code, you must use a "wrapper script" pattern that writes the result to a temporary file. This is a separate, advanced topic.
Practical Example: Launching Multiple Tasks in Parallel
This script demonstrates the power of background execution by starting three separate, time-consuming tasks at the same time. The total time to run the script will be only 5 seconds, not 15.
@ECHO OFF
ECHO --- Launching Parallel Tasks ---
ECHO All three tasks will be started at once.
ECHO.
START "Task 1" CMD /C "TIMEOUT /T 5 && ECHO Task 1 is complete."
START "Task 2" CMD /C "TIMEOUT /T 5 && ECHO Task 2 is complete."
START "Task 3" CMD /C "TIMEOUT /T 5 && ECHO Task 3 is complete."
ECHO All tasks have been launched by the main script.
Conclusion
The START command is the definitive tool for running commands and applications in the background from a batch script, enabling asynchronous and parallel execution.
Key takeaways for its effective use:
- The basic syntax is
START "Title" command. - Always include the "Title" argument, even if it's just empty quotes (
""). - To run internal commands like
DIRorCOPY, useSTART CMD /C "...". - To run a command with no new window, use the
/Bswitch. - Be aware that you cannot directly get the exit code of a process launched with
START.