How can I get a powershell script to throw an error

Dec 9, 2011 at 8:59 PM

I have a situation where I run a powershell script to display and error message to the user. For example, I want to be able to send an email message indicating a failed deployment if the user goes directly to a "QA" build quality.  The Deployment Mapping is:

  <Mapping BuildDefinitionPattern="v1.0"      
    Queue="DefAQueue" />  


The script merely does a write-host "Error:  You cannot go directly to QA .."

message. The issue is that the email says success, because the script ran successfully, but I want the email to state "Failed".  How can I force a failed situation.  I have tried "exit 1" in the script, but the invoking code does not pick it up.  I have forced an error condition that displays a ERROR in the email, that is irrelevant to the situation.  How can I cleanly display my output and force a failed email message

Dec 9, 2011 at 11:34 PM

I would recommend either:

throw "You cannot go directly to QA"


Write-Error "You cannot go directly to QA"

See which one works best for you.

You may also want to start your scripts with the following line so that they auto-abort when an error occurs instead of the default batch-like behaviour to continue running.

$ErrorActionPreference = 'Stop'