Creating Better PowerShell Output Part 1
There are some crucial guidelines you should follow when creating PowerShell-based tools. Presumably, you’ve written a PowerShell function that does something. We often create PowerShell functions to get something and display that something. You shouldn’t be in the mindset of returning a value. That’s what we did in VBScript and how many developers approach programming. But we are writing PowerShell code that follows a different paradigm. Our code should write objects to the pipeline. Sure, you can write a function that returns a value. There’s nothing illegal about that in PowerShell. But it isn’t the right way. Let’s look at why.
Return vs. Write
First off, you should take a few minutes to read the help topic about_return
. I am not a big fan of the return
keyword, except in exceptional cases, which I’ll cover later. I’ll admit that often its use doesn’t alter the outcome of your code. It is more of a principal. Let’s take a look.