Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I execute two PowerShell commands such that I only send text to the prompt once and the second command executes only after the first command has executed successfully?

share|improve this question
I suggest you share your code and tell us why it doesn't work as you'd like. You may need one of the suggestions from @Bob's comment or we may be able to fiddle with your pipeline to get similar results. – uSlackr Mar 15 '12 at 14:45
I don't have anything to share, as I had no idea how to do it, and my search efforts failed. – lance Mar 15 '12 at 14:48
up vote 1 down vote accepted

Try this:

[command1] ; if ($Lastexitcode -eq 0) { [command2] }

This executes command1 and then command2 if $Lastexitcode equals 0. If command1 produces an error, it should throw that error and stop executing anyway (if it is a powershell command - if not, it depends).

Is this what you were looking for?

Edit: The links in Bobs comment also deliver detailed solutions to your question.

share|improve this answer

Your Answer


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.