Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I configure help and usage statements in a powershell script?

Suppose I have the powershell script foo.ps1:

<# test foo.ps1 #>
param(
  [CmdletBinding()]
  [Switch]$foo,
  [Switch]$bar
)

Which has a few automatic parameters like -Debug and -Verbose in addition to the custom parameters -foo and -bar.

I should be able to see all of these parameters when I do something like .\foo.ps1 --help, or I should be able to configure that somehow.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


What is the core way to do this in powershell?

>Solution :

Get-Command will return you the full list of parameters, including common parameters:

$myScript = Get-Command .\foo.ps1

# this is a dictionary containing all the parameters
$myScript.Parameters 

# we can index into it like any other dictionary to inspect the parameter metadata for a given parameter
$myScript.Parameters['bar']

With regards to Get-Help, that part is already taken care of for you – when you do Get-Help .\foo.ps1 or .\foo -? after adding a CmdletBinding, PowerShell will automatically add [<CommonParameters>] to the command syntax string to indicate that common parameters are accepted:

Without [CmdletBinding()] decorator:

f [-foo] [-bar]

With [CmdletBinding()] decorator (or any parameter attribute decorators) specified:

f [-foo] [-bar]  [<CommonParameters>]

If you want richer help content displayed, then you’ll need to provide it 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading