PowerShell: Function Inline Doc

By Xah Lee. Date: . Last updated: .

Comment Based Help

A function can have embedded documentation. When user type help function-name, the documentation will show it.

Some part of the doc is auto generated.

Microsoft call this document system “Comment based help”.

# example of function with Comment Based Help

function f {
# .DESCRIPTION
# Add two numbers.
param ($x , $y)
$x + $y
}

When you type help f, it shows like this:

PowerShell help output 2024-03-13
PowerShell help output 2024-03-13

Help Comment Syntax

In order to be recognized as a help text, the comment must start with a line that is a dot followed by a certain keywords.

#.KEYWORD
#some thing

# Block comment can also be used:

<#
.KEYWORD
some thing
#>

[see Comment Syntax]

Help Comment Keywords

The following are most important keywords. (there are few others more esoteric.)

.DESCRIPTION

Most useful.

.SYNOPSIS

Best not use. Let it auto generate.

.PARAMETER

Best not use. Let it auto generate.

.EXAMPLE

Can have multiple.

.INPUTS

description.

.OUTPUTS

description.

.NOTES

notes.

.LINK

A URL.

Help Comment Position

The comment can appear immediately above the function:

# .DESCRIPTION
# Add two numbers.
function f {
    param ($x , $y)
    $x + $y
}

or in the beginning of the function block:

function f {
    #.DESCRIPTION
    # Add two numbers.
    param ($x , $y)
    $x + $y
}

or before the end of block:

function f {
    param ($x , $y)
    $x + $y
    #.DESCRIPTION
    # Add two numbers.
}

PowerShell: Define Function