Run multiple commands in one line in Powershell and Cmd


👤 Diwas Poudel    🕒 27 May 2023    📁 TECH

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 and typing.

We called those combining two or more commands into one line 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. So, we can say they execute serially one by one.

In Powershell

The use case of the 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 the PowerShell console.

Example 2:

ipconfig /release ; ipconfig /renew 

This will release the 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 fails 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, and 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 won't be executed. If you want each command to be executed only if the previous one was successful, combine them with the && operator.

You can use this in both CMD and Powershell.

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.

If you want to run the above command in PowerShell then try this:

(mkdir MyNewFolder ) -and (cd MyNewFolder )

and it does the same as in the command prompt.

In Powershell 7 Preview 5+:

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

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

Using | Operator

If you want to use the output of one command as the input of another command then you can use the pipe(|) operator.It is used extensively in Windows Powershell.

ie. The output of the first command acts as input to the second command and the output of the second to another command is connected via a pipe.

Example:

command1 | command2 | command3

Here, the output of command1 is input to command2, and the output of command2 acts as input to command. Then the result is displayed in the console because there are no more commands in the pipeline.

Practical Example:

This will list all the processes and then all the processes are sorted by CPU usage in ascending order and the last 5 records of the process will be taken. This diagram depicts the top five processes that consume the most resources.

Get-Process | sort CPU | select -last 5
pipe-operator-powershell
fig. pipe operator in Powershell

Using Grouping Operator in Powershell

This grouping operator is used to group multiple commands in a single scope. For example:

(dir; echo "Current directory Contents") > temp.txt

This command will group the content of the current direction and write them back to temp.txt file.

Using Redirection Operator 

There are various types of redirection operators and they are as follows:

< >  >|  <<  >>  <&  >&  <<-  <>

Redirections are handled in the order in which they appear, from left to right.

Using '>' redirection operator 

> operator writes to a new file.

Example:

ipconfig /all > yournetworkdetails.txt

This will the output of ipconfig /all command in yournetworkdetails.txt file as shown below.

use-of-left-arrow-operator-in-cmd
fig. showing output in the file generated by using < operator in cmd

Note that if there are no yournetworkdetails.txt files then it will create it.

Using '>>' redirection operator 

>> operator appends to a file.

Using '<' redirection operator

< operator input from a file.