How To Run Two Commands In One Line In Windows CMD

You can run multiple commands in one command line in the Windows Command Prompt (CMD).

Using conditional processing symbols, you can create multiple commands with just one command line or script. When you use the conditional processing symbol to execute multiple commands, the instructions to the right of the conditional processing symbol act on the results of the command to the left of the conditional processing symbol.

For example, you can run a command only if the previous command failed. Alternatively, perform the command only if the previous command was successful.

Running some commands with the character “&”

command1 & command2
echo foo & echo bar

The “&” character can separate multiple commands on one command line. Cmd.exe will run the first command, and then the second command.

Running some commands with the character “&&”

command1 &&command2
echo foo &&amp amp echo bar

Using the “&&” character, CMD will execute the first command (command1) first. If the first command completes successfully, a new second command (command2) will be executed.

Run some commands with the character “|| “

command1 II command2
echo foo || echo bar

If using the “||” character, CMD will run the first command (command1). The second command (command2) will only be executed if the first command did not complete successfully (received an error code greater than zero).

Note:

Ampersands (&), pipes (|), and parentheses () are special characters that must be preceded by an escape character (^) or quotation marks when you pass them as arguments.

Adding the escape character “^” before the command symbol allows it to be treated as plain text. Characters that usually have this special meaning can be passed and treated like ordinary characters: & \ < > ^ |

Echo THIS ^& THAT
Echo Heading1 ^| heading2 ^| heading3

If a command successfully completes the operation, it returns zero exit code (0) or no exit code. For more information about exit codes.

Latest Articles