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

309

40

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.

David

Posted 2013-06-26T17:24:22.883

Reputation: 6 593

To be nerdy... It's very easy to search for it. It's just quite hard to get a relevant set of hits. :) (+1 for great question) – Konrad Viltersten – 2014-06-27T16:01:59.503

2

Similar to conditional execution (&& and ||) in powershell.

– JamesThomasMoon1979 – 2018-12-29T22:30:33.593

5Fun Note: Between Serial ports and Serialisation, this question is virtually impossible to search for. – David – 2013-06-26T17:25:20.260

Answers

438

Use a semicolon to chain commands in PowerShell:

ipconfig /release; ipconfig /renew

Squeezy

Posted 2013-06-26T17:24:22.883

Reputation: 5 930

10Will they run in parallel or sequentially? – Tarkus – 2014-07-16T01:13:44.450

19This will run them sequentially, as does the & operator in cmd.exe. – Squeezy – 2014-07-23T05:38:46.883

47There is big difference though - ";" runs the second command even if the first fails. – Ivan – 2014-10-08T16:50:59.127

10As mentioned above, this is also the behavior of & in cmd.exe. – Squeezy – 2014-10-08T16:53:29.027

The second command won't run if the first throws a terminating error. In <throw "error";write-host "no error">, "no error" is never written to the console. – Dave_J – 2015-07-22T10:59:44.170

4There is a difference here in using the throw keyword ( throw being a keyword is the important part here ) directly and running a script that throws an error. <powershell -c "throw 'error'"; write-host " no error"> will run both commands sequentially. This happens even though the first command throws a terminating error. – Squeezy – 2015-07-22T11:08:21.723

7Is there a way to have the second command run only if the first fails? – Rafi – 2016-03-22T07:08:46.613

8@Rafi Yes, Try {Command-One -ea Stop} Catch {Command-Two} – Dave_J – 2016-08-05T11:05:47.540

32

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.

Dave_J

Posted 2013-06-26T17:24:22.883

Reputation: 431

Doesn't seem to work from "Target" field with -command option in shortcut though – clearlight – 2018-04-06T13:34:49.127