Doing More With Functions: Comment-Based Help


I just wanted to throw together a post highlighting how cool and easy it is to add help data to your own Functions and scripts.

The help data gets added via comments. For functions the help data can go in three places:

  1. Before the function keyword (I like it up here)
  2. Between the open curly brace and the param() statement
  3. At the bottom of the function before the closing curly brace (I hate this spot)

For scripts we just put it at the top of your script before you type the param() statement, or at the bottom, but the bottom will mess with code signing.

Syntax just involves using a dot before the help keyword and then typing the help you want for it on the next line:

<#
.<keyword>
<help data for it>
#>

Here is a list of all the keywords I found and short descriptions of them:

<#
.SYNOPSIS
A brief description of the function or script. 
This keyword can be used only once in each topic.

.DESCRIPTION
A detailed description of the function or script. 
This keyword can be used only once in each topic.

.PARAMETER <Parameter-Name>
The description of a parameter. 
Add a ".PARAMETER" keyword for each parameter in the function or script syntax.

.EXAMPLE
A sample command that uses the function or script, optionally followed by sample output and a description. 
Repeat this keyword for each example.

.INPUTS
The Microsoft .NET Framework types of objects that can be piped to the function or script. 
You can also include a description of the input objects.

.OUTPUTS
The .NET Framework type of the objects that the cmdlet returns. 
You can also include a description of the returned objects.

.NOTES
Additional information about the function or script.

.LINK
The name of a related topic. 
The value appears on the line below the ".LINK" keyword and must be preceded by a comment symbol # or included in the comment block.
Repeat the ".LINK" keyword for each related topic.

The "Link" keyword content can also include a Uniform Resource Identifier (URI) to an online version of the same help topic. The online version opens when you use the Online parameter of Get-Help. The URI must begin with "http" or "https".

.COMPONENT
The technology or feature that the function or script uses, or to which it is related. 

.ROLE
The user role for the help topic. 

.FUNCTIONALITY
The intended use of the function. 

.FORWARDHELPTARGETNAME <Command-Name>
Redirects to the help topic for the specified command. 
You can redirect users to any help topic, including help topics for a function, script, cmdlet, or provider.

.FORWARDHELPCATEGORY <Category>
Specifies the help category of the item in "ForwardHelpTargetName". 
Valid values are "Alias", "Cmdlet", "HelpFile", "Function", "Provider", "General", "FAQ", "Glossary", "ScriptCommand", "ExternalScript", "Filter", or "All". 
Use this keyword to avoid conflicts when there are commands with the same name.


.REMOTEHELPRUNSPACE <PSSession-variable>
Specifies a session that contains the help topic. 
Enter a variable that contains a "PSSession".

.EXTERNALHELP
Specifies an XML-based help file for the script or function.

#>

For the most part, I recommend using a few of the most common and important ones, which I think are:

  • Description
  • Parameter for every unclear parameter
  • Examples to help your users

Let's try it

<#
.Description
This function takes in a message and writes it out to the screen in cyan.

.Parameter Msg
This is the message that will be written to the screen in cyan

.Example
HelpTest -msg "Hello world"

.Example
HelpTest "Hello World"

#>

function HelpTest
{
    Param($msg)
    Write-Host $msg -ForegroundColor Cyan
}

get-help helptest -showwindow

Notice PowerShell numbers the examples for us in case we add more or move them around.

To make tacking on the help data easier, I recommend you utilize snippets (ctrl+J OR edit->snippets) and make a custom one.

$helpText = @"
<#
.Description

.Parameter 

.Example

#>
"@

New-IseSnippet -Title "Help (Simple)" -Text $helpText -Author KoryT -Description "simple comment based help"

Well, that's all for now, hopefully this helps you polish off your tools and write help data the right way!

This was originally written as part of  my "PowerShell For Programmers" series, though I might link it from other stuff in the future as well.

Let me know in the comments if there is any particular topics you're looking for more help with!

If you find this helpful don't forget to rate, comment and share 🙂

Comments (0)

Skip to main content