2 Ways To Run Multiple Commands In CMD

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…

How to run multiple commands in command prompt

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.

  1. Open a new Notepad file that has no text written on it.
    2 Ways To Run Multiple Commands In CMD 1
  2. 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.
    2 Ways To Run Multiple Commands In CMD 2
  3. 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.
    2 Ways To Run Multiple Commands In CMD 3
  4. 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.
    2 Ways To Run Multiple Commands In CMD 4

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.

  1. 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
    2 Ways To Run Multiple Commands In CMD 5
    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.
  2. 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
    2 Ways To Run Multiple Commands In CMD 6
  3. 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
    2 Ways To Run Multiple Commands In CMD 7
Command switchFunctionExamples
&Run the second command regardless of whether the first command completing successfully or notipconfig /flushdns & ipconfig /renew
&&Run the second command only if the first command runs successfullyipconfig /flushdns && ipconfig /renew
||Run the second command only if the first command failsipconfig /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.

Latest posts
Patch Tuesday Windows 10
Windows 10 KB5032189 Fixes Touchscreen, Update Installation Issues

Protect your Windows 10 devices with KB5032189 that addresses zero-day vulnerabilities that are actively being exploited.

View post
Windows 11 Known Issues and Solutions
Windows 11 Latest Known Issues And Their Fixes

Each Windows update comes with its own set of issues. Learn what problems there are in the latest Windows updates before installing them, and bypass/fix them if needed.

View post
Patch Tuesday Windows 11
Windows 11 KB5032190 Update Fixes 3 Vulnerabilities, Enables Moment 4 Features

Patch up the zero-day vulnerabilities in Windows 11 with KB5032190.

View post

Leave the first comment