Problem
You want your script to notify its caller of a warning, error, or terminating error.
############################################################################## ## ## GetWarningsAndErrors.ps1 ## ## Demonstrates the functionality of the WriteWarning, WriteError, and throw ## statements ## ##############################################################################
WriteWarning "Warning: About to generate an error" WriteError "Error: You are running this script" throw "Could not complete operation."
Solution
To write warnings and errors, use the WriteWarning and WriteError cmdlets, respectively. Use the throw statement to generate a terminating error.
Discussion
When you need to notify the caller of your script about an unusual condition, the WriteWarning, WriteError, and throw statements are the way to do it. If your user should consider the message as more of a warning, use the WriteWarning cmdlet. If your script encounters an error (but can reasonably continue past that error), use the WriteError cmdlet. If the error is fatal and your script simply cannot continue, use a throw statement.