Locations for Comment-based Help in PowerShell Functions

One of the first things you’ll learn when beginning with PowerShell is how to use the help system. When working from the PowerShell console, I use the help function and omit the Name parameter since it’s positional and then specify the name of the cmdlet that I’m looking for help on as shown in the following example.

help-locations1a

This same type of standardized help can be added to your PowerShell functions and scripts which makes it easy for others to learn how to use them. It’s called comment based help and it can be placed in one of three locations but there are some caveats to two of the options.

Me personally, I prefer to write my code in such a way that it works the same way no matter what the scenario is and it operates trouble free even when someone else needs to modify it. This is why I personally choose to place my comment based help just inside the function declaration:

If someone downloads a copy of one of my functions from GitHub and accidentally places multiple carriage returns before or after the comment based help, it still works without issue. I’ve also found that the average PowerShell script module creator places multiple functions inside of a script module file (PSM1) and this options allows anyone to clearly be able to determine what help goes with which function and it also allows the function and its help to be collapsed together.

Another option is to place the comment based help prior to the function declaration:

There’s two reasons I don’t like this option. If more than one empty line exists between the help and the function, it simply won’t work as PowerShell assumes the help is intended for the script itself and not the function. This option also doesn’t allow the help to be collapsed with the function so if you happen to place more than one function inside a PSM1 script module file, it’s less than optimal (at least in my opinion).

The third option is to place the help at the bottom of the function just inside the closing curly brace for the function:

While this location is valid for functions, it isn’t valid for scripts when they’re digitally signed which means my code couldn’t be written this way in all scenarios so that particular option is out since I want all of my code written in a standardized format.

Ultimately where you decide to place comment based help is a personal preference and you should follow your organizations standards or the project creators standards if you’re contributing to someone else’s code. See the about_Comment_Based_Help article on MSDN for more information on comment based help.

Know of any additional reasons why you would use one option over another? Feel free to post them via a comment to this blog article.

µ

Leave a Reply

%d bloggers like this: