PowerShell one-liners sometimes are an easy way to achieve something quickly. There are cases where you might want to run multiple separated PowerShell commands on one line. This can be handy if you do copy pasting of commands or if you want to make it easy for the end-user when you need to troubleshoot something. In this blog post, we will look at how you can chain and run multiple PowerShell commands on one line using pipelines and chaining commands.
In many cases, you will be happy to use pipelines in PowerShell. A pipeline is a series of commands connected by pipeline operators (|). Each pipeline operator sends the results of the preceding command to the next command.
For example, you can do things like get a process and then stop that process.
Get-Process "notepad" | Stop-Process
This will get the notepad process and pipe it to the next command, which will then stop that process. This is a great way to get the result from one PowerShell command to the other if these commands are related.
Chaining multiple PowerShell commands in one line
But what about if these commands are not related and have nothing to do with each other? In bash for example we can use
&& to chain commands and put two commands together. In PowerShell, we can achieve a similar thing using
firstcommand; secondcommand. Expect that in this case, it will run the second command even if the first command fails. So for example in PowerShell you can use chaining:
Get-Process "notepad"; start "https://www.thomasmaurer.ch"
In this case, we still do the first task, which is getting the notepad process, but we also want to open a website. If you can, it really makes sense to use PowerShell pipelines. However, if you want to do run multiple unrelated commands on the same line, you can use the
firstcommand; secondcommand method. In this case, the second command gets executed even if the first command fails.
This is also handy if one of the commands, for example, is a PowerShell command and the other might just a command-line tool.
This is nothing new and is working with Windows PowerShell 5.1 (and earlier) as well as PowerShell 7.
Pipeline Chain Operators
One of the new features in PowerShell 7 are the && and || operators to chain pipelines based on conditions. These operators are known in PowerShell as pipeline chain operators. They work very simply. The
&& operator would execute the right-hand pipeline if the left-hand pipeline succeeded. The
|| operator would run the right-hand pipeline if the left-hand pipeline failed.
Get-Process "notepad" && start "https://www.thomasmaurer.ch"
If the notepad process was running, it would open the website. If notepad isn’t running, this command would just provide an error that the process “notepad” could not be found.
Get-Process "notepad" || Start-Process notepad
With this command, we would see if the notepad process is running. If it is running, the second command doesn’t get executed. If the notepad process is not running, this would now run the second command, which would start the notepad process.
You can see there are many different ways you can chain commands in PowerShell depending on your needs. If you have any questions, feel free to leave a comment.Last modified: March 25, 2021