How to enable a PowerShell script to return help text when using Get-Help or -?

Question:

How do I get my PowerShell script to display help when a user requests help via:

or

For example, to describe my scripts parameters etc.

Updated question

I tried the suggested answers. But I don’t see the expected output.

I simply place added the following code to myScript.ps1.

When I tired

Get-Help .\myScript.ps1

, it shows as follows

I was expecting to see the description of the parameters. I tried both.PARAMETER <-silent> and .PARAMETER -silent. Same results. What is wrong?

Related question update 2 after processing more

I can see the help file after adding parameter section as follows:-

But very important to see full help file I must use
Get-help .\myscript.ps1 -full
Otherwise, it doesn’t show full help information.

But here comes my next question. When I tried -full. It shows some irrelevant information. I would like to omit this information to be shown to the user. Now it displays as following:-

NAME
C:\es\dev\myscript.ps1

SYNOPSIS
It performs Collection Bootstrapping silently or parameterized based.

SYNTAX
C:\es\dev\myscript.ps1 [[-action] ] [[-file] ] [[-sasHostname] ] []

DESCRIPTION
For silent operation, user has to fill configuration file with desired value at Eurostep.SAS.Collection Bootstrap.Config.psd1. This
configuration file has to exist in
the same directory of Eurostep.SAS.CollectionBootstrap.ps1 PowerShell script.

PARAMETERS
-action

INPUTS

OUTPUTS

RELATED LINKS

I don’t want to show RELATED LINKS INPUTS OUTPUTS , etc.

Is there anyway to remove them from help informtion. Even -Full is used?

Answer:

The simplest way to do this is to add comment-based help to your script. This is a special comment block that is formatted with specific keywords and then the associated text for those keywords. For example:

These are the keywords I tend to use by default but look at the full list described here for others you might want to include: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comment_based_help?view=powershell-5.1

Source:

How to enable a PowerShell script to return help text when using Get-Help or -? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply