How do I run multiple commands on one line in PowerShell?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor
--
Chapters
00:00 How Do I Run Multiple Commands On One Line In Powershell?
00:32 Accepted Answer Score 788
00:42 Answer 2 Score 63
01:50 Answer 3 Score 37
02:33 Answer 4 Score 14
04:01 Thank you
--
Full question
https://superuser.com/questions/612409/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#windows #commandline #powershell
#avk47
ACCEPTED ANSWER
Score 788
Use a semicolon to chain commands in PowerShell:
ipconfig /release; ipconfig /renew
ANSWER 2
Score 63
In PowerShell 7, we have Pipeline chain operators
which allows you to add some conditional element to your sequential one-line commands
The operators are:
&&
this will run the second command only if the first one succeeds.||
this will run the second command only if the first one fails.
examples:
C:\> Write-Host "This will succeed" && Write-Host "So this will run too"
This will succeed
So this will run too
C:\> Write-Error "This is an error" && Write-Host "So this shouldn't run"
Write-Error "This is an error" && Write-Host "So this shouldn't run": This is an error
C:\> Write-Host "This will succeed" || Write-Host "This won't run"
This will succeed
C:\> Write-Error "This is an error" || Write-Host "That's why this runs"
Write-Error "This is an error" || Write-Host "That's why this runs"
This is an error
That's why this runs
of course you can chain them even more together like x && y || z
etc.
this also works for old cmd-like commands like ipconfig
> ipconfig && Write-Error "abc" || ipconfig
Windows-IP-Konfiguration
Ethernet-Adapter Ethernet:
Verbindungsspezifisches DNS-Suffix: xxx
Verbindungslokale IPv6-Adresse . : xxx
IPv4-Adresse . . . . . . . . . . : xxx
Subnetzmaske . . . . . . . . . . : 255.255.255.0
Standardgateway . . . . . . . . . : xxx
ipconfig && Write-Error "abc" || ipconfig: abc
Windows-IP-Konfiguration
Ethernet-Adapter Ethernet:
Verbindungsspezifisches DNS-Suffix: xxx
Verbindungslokale IPv6-Adresse . : xxx
IPv4-Adresse . . . . . . . . . . : xxx
Subnetzmaske . . . . . . . . . . : 255.255.255.0
Standardgateway . . . . . . . . . : xxx
These operators use the $? and $LASTEXITCODE variables to determine if a pipeline failed. This allows you to use them with native commands and not just with cmdlets or functions.
ANSWER 3
Score 37
A semicolon will link the commands as the previous answer stated, although there is a key difference to the behaviour with the &
operator in the MS-DOS style command interpreter.
In the command interpreter, the variable substitution takes place when the line is read. This allows some neat possibilities such as swapping variables without an interim:
set a=1
set b=2
set a=%b% & set b=%a%
echo %a%
echo %b%
Would result in:
2
1
As far as I know, there is no way to replicate this behaviour in PowerShell. Some may argue that's a good thing.
There is in fact a way to do this in PowerShell:
$b, $a = $a, $b
It will result in a single line swapping of the variable values.
ANSWER 4
Score 14
For PowerShell 5 (default install for Windows machines for the foreseeable future), you can of course use a semicolon to separate statements, but all statements will be executed by default even if one fails. Personally, I prefer to run things so that if one thing fails the whole line stops in the REPL and I imagine a lot of other folks do as well.
$ErrorActionPreference
lets you control the behavior of what happens when a statement fails but is a non-terminating error (which are most errors including command not found errors). You can set this variable $ErrorActionPreference="Stop"
in order to emulate the behavior of &&
in Bash and PowerShell 7 for the scope of this variable.
$ErrorActionPreference="Stop"
# Line break
fakeCommand; echo "Here"
I have had trouble finding precise documentation for this behavior, but this variable seems to be dynamically scoped so you can override it in a block temporarily if you don't want to set it globally.
Invoke-Command -ScriptBlock {$ErrorActionPreference="Stop"; fakeCommand; echo "Here"}
Finally, if you want something reusable, you can use this higher order function.
function Run-Block-With-Error($block) {
$ErrorActionPreference="Stop"
Invoke-Command -ScriptBlock $block
}
Which is then used as follows.
Run-Block-With-Error {fakeCommand; echo "Here"}
Note in the examples above that "Here" is not printed since fakeCommand
fails as it is not a real command.
I have tested the code provided in this solution for both PowerShell 5 and 7 and it should be fully portable, at least on Windows. While PowerShell 7 should be very similar on different platforms, I did not test these commands on Linux or MacOS.