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.
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 🙂