Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Windows' native CMD processor, you can use & to string commands together so that one runs immediately after the other. A && will run the second command only if the first completes without error.

This is different from piping commands together with | in that output from the first command is not sent to the second command, the commands are just simply run one after the other and output for each is sent to its usual place.

However, I get an error when I try to use & or && in PowerShell. Are there similar functions available in PowerShell, or is this feature being deprecated?

share|improve this question
2  
    
An interesting question is if powershell's metaprogramming could be used to provide this functionality using && –  soandos Aug 20 '12 at 21:08

2 Answers 2

The & operator in PowerShell is just the ; or Semicolon.

The && operator in PowerShell has to be run as an if statement.

Command ; if($?) {Command}

share|improve this answer

Try this function, which can be used roughly the same way:

function aa() {
    if($?) {
        $command = [string]::join(' ', $args[1..99])
        & $args[0] $command
    }
}

Now && can be replaced with ; aa, which while still not perfect is a lot more succinct.

cls && build

becomes

cls; aa build
share|improve this answer
    
Any non-stylistic reason why you're using args[0] and not function aa ($cmd)? –  ruffin Nov 25 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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