How to Tell if Cmdlet Parameters are Mandatory or Optional?
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
This is not a bug report, but rather a question on how to interpret the PowerShell documentation (man pages).
My question is: How does one tell which cmdlet parameters are required, and which are optional?
I know how mandatory parameters are declared in code ([Parameter(mandatory = true)]), but not how to determine that from the documentation Web pages.
For example, the Write-Progress page (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-progress?view=powershell-7.3) shows A number of parameters, but the -Activity parameter is not enclosed in square brackets like the others. Does that mean the -Activity parameter is mandatory and the others optional? Or am I reading too much into the format?
Is there a way to determine which parameters for a cmdlet are required, other than trial and error experimentation?
Expected behavior
N/A
Actual behavior
N/A
Error details
No response
Environment data
PSVersion 7.3.4
PSEdition Core
GitCommitId 7.3.4
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Issue Analytics
- State:
- Created 3 months ago
- Comments:14 (2 by maintainers)
You can find mandatory parameters with
Get-Command
, for example inExport-Csv
:A cut and paste from @237dmitry 's example works for me. Get-Command itself doesn’t have any manadatory parameters.
(Get-Command write-progress).Parameters.Values.where{ $_.ParameterSets.Values.IsMandatory }.Name
returns “Activity” for me .
This builds on his example.
A cut and paste will show just how complicated
where-object
is. If you change where object to get-command or write-progress you’ll get something simpler.