How do I run multiple commands on one line in PowerShell?

In a cmd prompt, you can run two commands on one line like so:

ipconfig /release & ipconfig /renew

When I run this command in PowerShell, I get:

Ampersand not allowed. The `&` operator is reserved for future use

Does PowerShell have an operator that allows me to quickly produce the equivalent of & in a cmd prompt?

Any method of running two commands in one line will do. I know that I can make a script, but I'm looking for something a little more off the cuff.


Use a semicolon to chain commands in PowerShell:

ipconfig /release; ipconfig /renew

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.


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:

PS Z:\Powershell-Scripts> Write-Host "This will succeed" && Write-Host "So this will run too"
This will succeed
So this will run too

PS Z:\Powershell-Scripts> 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

PS Z:\Powershell-Scripts> Write-Host "This will succeed" || Write-Host "This won't run"
This will succeed

PS Z:\Powershell-Scripts> 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

PS Z:\Powershell-Scripts> 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.

Source: https://docs.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7