256

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.

370

Use a semicolon to chain commands in PowerShell:

ipconfig /release; ipconfig /renew
  • 7
    Will they run in parallel or sequentially? – Tarkus Jul 16 '14 at 1:13
  • 16
    This will run them sequentially, as does the & operator in cmd.exe. – Squeezy Jul 23 '14 at 5:38
  • 39
    There is big difference though - ";" runs the second command even if the first fails. – Ivan Oct 8 '14 at 16:50
  • 9
    As mentioned above, this is also the behavior of & in cmd.exe. – Squeezy Oct 8 '14 at 16:53
  • 7
    @Rafi Yes, Try {Command-One -ea Stop} Catch {Command-Two} – Dave_J Aug 5 '16 at 11:05
30

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.

  • Doesn't seem to work from "Target" field with -command option in shortcut though – clearlight Apr 6 '18 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.