Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get help messages to appear for my Powershell script parameters?

I have a powershell script (setup.ps1), that we use as the entry point for our development environment setup scripts. It takes a parameter:

param(     [Parameter(Position=0,HelpMessage="The targets to run.")]     [Alias("t")]     [string[]]     $Targets = "Help" ) 

When I run

PS > get-help .\setup.ps1 -detailed 

in the parameters section, my help message doesn't appear:

PARAMETERS     -Targets <String[]> 

What do I need to do to get my parameter help messages to display?

like image 625
Aaron Jensen Avatar asked Mar 08 '11 20:03

Aaron Jensen


People also ask

How do you show help in PowerShell?

Description. The Get-Help cmdlet displays information about PowerShell concepts and commands, including cmdlets, functions, Common Information Model (CIM) commands, workflows, providers, aliases, and scripts. To get help for a PowerShell cmdlet, type Get-Help followed by the cmdlet name, such as: Get-Help Get-Process .

How do I add help in PowerShell?

Adding Help to a Custom Cmdlet When you need help with a PowerShell cmdlet, you can type Get-Help followed by the cmdlet name. If for example, you needed help with the Get-Process cmdlet, you could type Get-Help Get-Process. The command shell would then show you the command syntax.

How do I set parameters in PowerShell?

To create a parameter set, you must specify the ParameterSetName keyword of the Parameter attribute for every parameter in the parameter set. For parameters that belong to multiple parameter sets, add a Parameter attribute for each parameter set.


1 Answers

You put a certain style of comment at the top of the file that can be decoded by the PowerShell help system. Here's an example:

<# .SYNOPSIS     . .DESCRIPTION     . .PARAMETER Path     The path to the . .PARAMETER LiteralPath     Specifies a path to one or more locations. Unlike Path, the value of      LiteralPath is used exactly as it is typed. No characters are interpreted      as wildcards. If the path includes escape characters, enclose it in single     quotation marks. Single quotation marks tell Windows PowerShell not to      interpret any characters as escape sequences. .EXAMPLE     C:\PS>      <Description of example> .NOTES     Author: Keith Hill     Date:   June 28, 2010     #> function AdvFuncToProcessPaths {     [CmdletBinding(DefaultParameterSetName="Path")]     param(         [Parameter(Mandatory=$true, Position=0, ParameterSetName="Path",                     ValueFromPipeline=$true,                     ValueFromPipelineByPropertyName=$true,                    HelpMessage="Path to ...")]         [ValidateNotNullOrEmpty()]         [string[]]         $Path,          [Alias("PSPath")]         [Parameter(Mandatory=$true, Position=0, ParameterSetName="LiteralPath",                     ValueFromPipelineByPropertyName=$true,                    HelpMessage="Path to ...")]         [ValidateNotNullOrEmpty()]         [string[]]         $LiteralPath     )     ... 

For more info see the help topic - man about_comment_based_help.

like image 152
Keith Hill Avatar answered Oct 11 '22 02:10

Keith Hill