What is the best practice for returning error from a PowerShell cmdlet?

Question:

I am writing a PowerShell-based XML module for our application configuration needs. Following is the one of the functions.

As this cmdlet will be consumed by many I don’t think simply writing into console will be the right approach.

As of now in my script if no errors, I can assume that mine is successfully completed.

What is the standard practice to get the results from a PowerShell cmdlet so that the consumer knows whether it is successfully completed or not?

Answer:

The standard practice is to throw exceptions. Each different type of error has a separate exception type which can be used to diagnose further.

Say, file is not represented, you do this:

Your cmdlet should document all the possible exceptions it will throw, and when.

Source:

What is the best practice for returning error from a PowerShell cmdlet? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply