Command Prompt is still a very useful tool when it comes to automating and completing tasks that are difficult to accomplish when working with a graphical user interface. As an example, I often clear the DNS and DHCP caches of my computers using the command line. While this task can be tedious when carried out visually, the command line can accomplish it in a matter of seconds.
In CMD, there are times when the user must run multiple commands simultaneously. The following are some situations in which you might find yourself, and each of them will be discussed in turn.
- Run multiple commands simultaneously
- Run multiple commands sequentially
- Only run the next command in the queue if the first executes successfully.
- Only execute the next command in the queue if the first failed to execute successfully.
These are a few use cases that we will address in this article.
Run multiple commands using the batch script
You can use batch scripts to run multiple commands and instructions on your machine simultaneously. Using a batch script, you will be able to execute all your commands one by one automatically. All you need to do is follow the steps below.
- Open a new Notepad file that has no text written on it.
- Enter all the commands in the Notepad file that you want to be executed together. For example, I want to flush DNS first, then I will renew it. For this, I have to run four commands one by one. But here, I have added all the commands in a single file.
- Now save this file with an appropriate name, and don’t forget to save the file with “.bat” extension. Otherwise, you won’t be able to run the script.
- Go to the location where you have saved the Batch Script file. Double click on it to run the file. This won’t take long to execute all your commands successfully.
This is a way to run multiple commands easily and quickly. I hope this will work for you. If you don’t want your commands to run with Batch Script, you may apply other solutions described below.
Run Multiple Commands Through &, &&, and || sign
With this solution, I am giving you three possibilities on how you want to run your multiple commands in CMD.
- The first one is using “&” Combine your two commands with “&” sign. This will allow your second command to run after the complete execution of the first command.
For example:
ipconfig /flushdns & ipconfig /renew
The second command will run as soon as the first command completely runs. It won’t matter that first command has run successfully or not. - But if you want to run the second command, after the success of the first command. Then, enter “&&” sign between your two commands.
ipconfig /flushdns && ipconfig /renew - The third case is, the second command will run only if the first command has not executed successfully. For this enter “||” between two commands.
ipconfig /flushdns || ipconfig /renew
Command switch | Function | Examples |
---|---|---|
& | Run the second command regardless of whether the first command completing successfully or not | ipconfig /flushdns & ipconfig /renew |
&& | Run the second command only if the first command runs successfully | ipconfig /flushdns && ipconfig /renew |
|| | Run the second command only if the first command fails | ipconfig /flushdns || ipconfig /renew |
You can also use a combination of switches within the same line. For example, cmd && success action || fail action & always run. All these commands can also be executed within a batch file. The same command line switches can also be used in PowerShell.
Hopefully this will help you automate your commands the way you want. The command line will save you lots of time and will give you flexibility in your work. How do you use the command line? Please share your thoughts in the comments below.