SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

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

I have SharePoint Powershell Script in .ps1 format and it includes 4 commands. When I run this script, it does not stop if any error occurs in middle.

For example, if error occurs at 2nd command it displays error message and starts executing the 3rd command, but I want it to stop at 2nd command and it should not execute any following commands.

share|improve this question

Press Ctrl + X to cancel the script execution.

share|improve this answer

You can specify -erroraction parameter value to stop. If you want same behavior for all commands in your script, set $ erroractionpreference value to stop before executing any commands.

share|improve this answer
i tried as below but same issue: the 1st command is : New-SPWebApplication -Name $name -ApplicationPool $applicationpool -ApplicationPoolAccount $applicationpoolaccount -Port $port -AuthenticationProvider $both -HostHeader $hostHeader -AllowAnonymousAccess -erroraction stop and the 2nd command is :New-SPSite -Url $URL -OwnerAlias $creden -Template $template -erroraction stop when there is error in firsty command it should not execute 2nd one. – ARD Nov 21 '11 at 5:43
still waiting for solutions..... – ARD Nov 24 '11 at 8:00

You might consider using try/catch blocks. An example on how to use this is in the first answer at:

share|improve this answer

You have to set ErrorAction Parameter to stop. To see which values are valid for the ErrorAction parameter you should use

Get-Help about_commonparameters

try-catch-finally is used as in many other programming languages

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.