How to add help in the PowerShell function?


When we write a program, people from a non-programming background often expect to get much possible help related to the program. When we write the function and we declare the parameters, people who are unaware of what kind of input the parameter need, generally search for the help first using the Get-Help command and then they find only the parameters but not the description of it. For example,

function TestFunct{
   param(
      #16 Digit Application ID
      [parameter(Mandatory=$true)]
      [String]$AppID,
      #Date in the Unix Format - 2020-10-31T17:12:10+0530
      [String]$Date
   )
}

In the above example, there are two parameters specified and when the user gets help with the function, it doesn’t specify the comments where the parameter description is given. For example,

PS C:\> help TestFunct -Parameter *
-AppID <string>

   Required?                   true
   Position?                   0
   Accept pipeline input?      false
   Parameter set name          (All)
   Aliases                     None
   Dynamic?                    false

-Date <string>

   Required?                   false
   Position?                   1
   Accept pipeline input?      false
   Parameter set name          (All)
   Aliases                     None
   Dynamic?                    false

To add the description from the comment, we need to add the comment-based help and need to use SYNOPSIS from the comment-based help.

Example

function TestFunct{
   <#
      .SYNOPSIS
      This is test function for parameter based help
   #>
   param(
      #16 Digit Application ID
      [parameter(Mandatory=$true)]
      [String]$AppID,
      #Date in the Unix Format - 2020-10-31T17:12:10+0530
      [String]$Date
   )
}

Now when we check the parameter, we get the comment based description.

PS C:\> help TestFunct -Parameter *
-AppID <String>
   16 Digit Application ID

   Required?                      true
   Position?                      1
   Default                        value
   Accept pipeline input?         false
   Accept wildcard characters?    false

-Date <String>
   Date in the Unix Format - 2020-10-31T17:12:10+0530

   Required?                      false
   Position?                      2
   Default                        value
   Accept pipeline input?         false
   Accept wildcard characters?    false

Updated on: 02-Nov-2020

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements