Run multiple commands in one line in Powershell and Cmd


👤 Diwas Poudel    🕒 Nov 27 2021    📁 Fix    📜 0 comment

If you are a Windows user, you are aware that Command-Line and PowerShell are the most powerful and useful tools for automating various complex administrative tasks. In most cases, we perform a single command/cmdlet per line, execute it, and then move on to the next line to create additional cmdlets. However, it will be even more efficient if we run two or more commands on the command line at the same time, saving you time.

We called those combining two or more commands into one line as Command Chaining.

In this tutorial, Ourtechroom will walk you through various methods for combining multiple commands efficiently.

Using Semicolon(;) Operator

The semicolon operator is a helpful operator that allows you to execute many instructions in succession, regardless of whether the previous command succeeded or failed. As a result, the semicolon is useful when the commands are not interdependent.

Usage:

command1 ; command2 ; command3 [; command4 ...]

There is no need for a space between each command, in this case, it is provided for better readability.

In this case, command1 will be executed first, followed by command2, regardless of whether command1 is executed successfully or not.Then, regardless of whether commands 1 and 2 succeed or fail, command 3 will be executed.

In Powershell

Usecase of semicolon operator in Powershell is as follows

Example 1:

mkdir TestFolder ;  ipconfig /all 

In this example, commands are executed sequentially from left to right. First, mkdir makes TestFolder in Current Path and then gets ipconfig information. These two commands run independently. Suppose if TestFolder already exists then mkdir will not make a folder but it will execute ipconfig and display it in PowerShell console.

Example 2:

ipconfig /release ; ipconfig /renew 

This will release ip assigned to the computer and renew the new ip.

Example 3:

Write-Host "Get File version" ; $fileversion = (Get-Command C:\Windows\System32\appwiz.cpl).FileVersionInfo.FileVersion ; Write-Host "File version -" $fileversion

Here we have 3 commands each separated by a semicolon. These commands are executed in the following order: print message, get file version, and print output. If one failed the other will execute.If all failed nothing will be executed.If nothing failed all will be executed.

Example 4:

Stop-Process -Name notepad ;  Get-Process cmd

If cannot stop a notepad process, then also we will get cmd process information.

In Command:

In the Command prompt, we do not have the use of a Semicolon separator to separate commands.

Using OR(||) Operator

Here, only the command after || will run if the command before || failed to execute. ie.Cmd.exe/PowerShell executes the first command first, then the second command only if the first command fails.

Usage:

command1 || command2 || command3 [|| command4 ...]

Here, Command2 runs only if command1 failed, command3 will run only if command1 and command2 failed and soon.

In Powershell:

To run || separator you will need Powershell 7 version.

writeeee-host "Syntax Error" || write-host "second command"

The first command syntax is a mistake so will execute the second command and will print out "Second Command"

In Command Prompt

Example 1:

cd Develop || mkdir Develop || echo "Develop directory not found and could not be created"

Here, if Develop directory is found then this whole command will just change the directory to "Develop".If Develop directory is not found then mkdir commands make Develop directory and if cannot create Develop directory then will print "Develop directory not found and could not be created".

Using AND(&&) Operator

If anyone left of an operator fails then all other commands located right of the operator wont executed. If you want each command to be executed only if the previous one was successful, combine them with the && operator.

Usage:

command1 && command2 && command3 [&& command4 ...]

Here, if command2 runs only if command1 succeeded. Also, command3 runs only if command1 and command2 succeed and soon.

In Command Prompt:

Example:

mkdir MyNewFolder && cd MyNewFolder

If the first command, mkdir, succeeds, MyNewFolder will be created in the current path and the path will be automatically changed and moved inside MyNewFolder.

In Powershell

&& operator is now available for PowerShell 7 Preview 5+:

echo "Hello!" && echo "World!"