Execute a command asynchronously while redirecting the output to a file in a batch file?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In a Windows batch file, the usual way to start a command asynchronously is start. To redirect the command output to a file at the same time, you normally wrap the real command in cmd /c, because redirection has to happen inside the new process rather than in the parent batch script.
Use start with cmd /c
The basic pattern looks like this:
This does three important things:
- '
startlaunches a new process without waiting for it to finish' - '
cmd /cruns the command and exits when it is done' - '
> output.log 2>&1redirects both standard output and standard error'
The batch file continues immediately after start, which is why echo Command launched appears right away.
Why the Empty Quotes Matter
One of the most confusing details in start syntax is that the first quoted string is treated as the window title, not as the command. If you skip the empty title and your command itself is quoted, start may misread the command line.
Correct:
Incorrect:
That small empty string prevents a lot of quoting bugs.
Redirect Output Inside the Spawned Command
Redirection must be inside the command string passed to cmd /c. If you place the redirection outside, you are often redirecting the parent batch script instead of the asynchronous child process.
Good:
Not what you usually want:
The first version keeps the redirection attached to the spawned command. The second version is much easier to misparse, especially once spaces and quoted paths are involved.
Append Instead of Overwrite When Needed
If you want the new command to append to an existing file instead of replacing it, use >>:
For long-running scheduled scripts, append mode is often the better default because it preserves previous runs.
Handle Commands and Paths with Spaces
Windows command lines become tricky as soon as executable paths contain spaces. Put the full command inside the cmd /c string and quote the executable path there.
The escaped quotes around the executable are necessary because they live inside the larger quoted string passed to cmd /c.
Know What /b Changes
The /b option tells start to run without creating a new visible console window. That is useful for background jobs launched from scripts.
If you omit /b, Windows may open a separate command window for the asynchronous task. Sometimes that is desirable for debugging, but it is usually noisy for automation.
Logging and Exit Codes
Because the command is asynchronous, the parent batch file does not automatically receive the child process exit code. If you need to know whether the child succeeded, you usually need one of these patterns:
- have the child write a success marker file
- log the exit code from inside the
cmd /cstring - run synchronously instead of asynchronously
Example of logging the exit code:
That works inside a batch file because %%errorlevel%% is expanded by the child command interpreter.
Common Pitfalls
The biggest mistake is forgetting that the first quoted argument to start is the window title, not the command path. Another common problem is placing redirection outside the cmd /c string, which redirects the wrong process. Quoting paths with spaces is also easy to get wrong because there are multiple layers of command parsing involved. Finally, asynchronous execution means you cannot rely on the parent batch file's %errorlevel% to tell you whether the launched command succeeded.
Summary
- Use
start "" /b cmd /c "your command > file 2>&1"to run a command asynchronously and capture its output. - Keep the redirection inside the quoted
cmd /ccommand string. - Include the empty title argument after
startso quoting works correctly. - Use
>>if you want to append to the log file instead of overwriting it. - Remember that asynchronous commands do not give the parent batch file a normal synchronous exit code.

