In previous article, I mentioned How to Check Motherboard Version with cmd and PowerShell but now in this article, I have mentioned how we can run multiple commands using Powershell, using semicolon or using OR operator.

PowerShell is one of the tools used by Windows users to execute numerous complex administrative tasks. Most people perform a single command or cmdlet on each line and then move to a new line to create more cmdlets.

However, this leaves you writing so many lines to execute one task. You also spend more time and type extra for every new line you create.

For more efficiency, you can decide to run multiple commands on a single line with a process called command chaining. This article will guide you on running multiple command on a single line in PowerShell. 

How To Run Multiple Commands On A Line In PowerShell

The five methods used to run multiple commands on a single line in PowerShell are:

Use the Semicolon Operator (;)

run-multiple-commands-on-powershell-at-once

The Semicolon operator is helpful when you need to run two or more commands on a line. These commands are executed in succession whether the preceding command is successful or not. This makes the Semicolon operator very useful in the execution of independent commands. 

Here is what the command line will look like:

Command1 ; Command2 ; Command3

The above is a command line with three commands separated by Semicolon operators, and it can work for as many commands as you want. 

These commands are executed progressively from left to right, meaning Command1 is executed first, followed by Command2, and so on. Whether the execution of Command1 fails or not, Command2 will be automatically executed, and the chain keeps going on.

If all commands are unsuccessfully executed, nothing will be executed but if all commands are successfully executed, everything will be executed.

You can use the Semicolon operator in every version of PowerShell.

Note that you do not need spaces before or after the semicolon as they are unnecessary and only give you more reasons to use the unneeded space bar. The spaces here are only to improve readability.

Use the OR Operator (||)

a2.png

The OR operator also comes in handy to run multiple commands on a line. When you use the OR operator, the commands are executed in order, and commands will be executed whether the one preceding it is successfully or not. 

Here is the outline of a multiple command line with the OR operator.

Command1 || Command2 || Command3

The outline above represents three commands separated by OR operators, but you can use it for more commands.

The commands are executed from left to right and Command2 is only executed if the execution of Command1 is a success or a failure. Command3 also follows suit after the execution of Command3 has been deemed a success or failure.

However, the OR operator does not work for every version of PowerShell, you need the Powershell 7 version to use the OR operator. 

Also, note that you do not need to add spaces before or after OR operators. They do not have any impact on your command and only make you type more. Spaces in the outline here are to help you read better.

Use the AND Operator (&&)

a3.png

You can use the AND operator when you need to undergo command chaining. When you use the AND operator, the system will only execute a command if the command preceding it is successful.

Here is the outline of a multiple command line with the AND operator.

Command1 && Command2 && Command3

Again, the outline is A command line with three commands separated by AND operators. But, you can use the method for more commands on the same line.

In this method, if Command1 executes successfully, then Command2 will run. If Command2 is then successfully, Command3 will follow suit.

Use the AND operator only when you want to ensure the execution of the command should come after the one before it has been successful.

It is available on Powershell 7 Preview 5+ and all versions of PowerShell. Do not forget that you should not put spaces before and after AND operators as it is unnecessary.

Use The Pipe Operator (|)

a4.png

Using the Pipe operator is another method to separate multiple commands written on a line. When you use the Pipe operator, the output of a command will be the input of the command succeeding it.

For instance, in the outline below, the output of command1 will be the input for command2, etc. 

Command1 | Command2 | Command3

The outline here also has three commands separated by Pipe operators. The vertical bar (pipe) that separates the command is single here, but double when using the OR operator. 

The output of Command1 is the input of Command2 and the Output of Command2 is the input of Command3. The pipe operator is referred to as a pipe and the chain of commands is called a pipeline.

Once there are no more commands at the end of the pipeline, the result is displayed in the console.

Again, you do not need to use unnecessary spaces before and after the vertical bars.

Use Redirection Operators

Redirection operators are special characters you can use with a command to either redirect input to it or redirect the output from it. Redirections are orderly executed from left to right.

There are different types of Redirection operators:

  • The ">" Redirection operator writes to a new file.
  • The ">>" Redirection operator also appends to a new file. It is like the ">" Redirection operator but does not overwrite the old file.
  • The "<" Redirection operator inputs from a file.

Conclusion

With these methods for running multiple commands on a line in PowerShell, you can successfully conserve time and use lesser lines to execute complex tasks.

You may also like to read:

How To Check Bios Version in CMD 

How to Add Background Image to Google Calendar Event

Best WinMerge Alternative Diff Tool (Free and Open Source)

Best snipping tool for Windows or Mac (Free or paid)

How To Monitor CPU Temperature In Windows 10?