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.
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.
Usecase of semicolon operator in Powershell is as follows
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.
This will release ip assigned to the computer and renew the new ip.
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.
If cannot stop a notepad process, then also we will get cmd process information.
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.
Here, Command2 runs only if command1 failed, command3 will run only if command1 and command2 failed and soon.
To run || separator you will need Powershell 7 version.
The first command syntax is a mistake so will execute the second command and will print out "Second Command"
In Command Prompt
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.
Here, if command2 runs only if command1 succeeded. Also, command3 runs only if command1 and command2 succeed and soon.
In Command Prompt:
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.
&& operator is now available for PowerShell 7 Preview 5+: