Force Powershell script to Stop but leave the console open to review

I have a form that opens, with powershell, and also the console opens so you can see the verbose output while the forms operations are performed, based on what button is clicked. And if an error occurs that powershell recognizes, it stops as expected. But if something in my logic errors, the script continues to run as if nothing happened, so I’m trying to figure out how I can throw an error and stop the script from running without actually closing the console.

EXIT doesn’t seem to be the correct choice, because it will close the console, is there an alternate solution?

Typically you’ll do this with a “throw” statement. This produces a terminating error that will abort your script, unless your code handles it with a try/catch block somewhere.

Nice, I was just testing with using -ErrorAction Stop like this:

Write-Error -Message "`$CopyDirection value must be equal to `"ToNetwork`" or `"ToLocal`"" -ErrorAction Stop

That seemed to work ok, but I like the Throw one too.

What is the difference, is one more commonly used then the other, is one more recommended than the other?