1

I am using Powershell as my Console. I created a PSSnapin and added it to Powershell. Now i want that for some exception, it should exit there, but since i have called it from some function, it returns back there which i don't want. I want it to exit at that instant only without closing the powershell Console. I tried Environment.Exit(0) but it is closing the console application. Is there any other way to exit?

1
  • What happens if you call Dispose on your snapin instance? Commented Mar 22, 2011 at 5:47

1 Answer 1

2

Snapins run in the PowerShell process and as such, PowerShell attempts to protect itself by catching exceptions that a snapin throws. The last thing you want to do is call Environment.Exit() because that will kill the PowerShell process. You can either let the exception escape from your cmdlet implementation (or throw if necessary) but it would be better to use the ThrowTerminatingError() method on your PSCmdlet instance to signal a terminating condition for your cmdlet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.