In PSH V2, you can do this with parameter attributes and put two parameters in different parameter sets.
A parameter set is a group of parameters that go together, a command can have several sets of parameters, and only one is available. Parameters not assigned to a parameter group are available for all parameters. This can be seen in standard cmdlets (deleting common parameters):
PS> gcm -syn get-content
Get-Content [-Path] ...
Get-Content [-LiteralPath] ...
To achieve this in a script or function:
- Add
[CmdletBinding] as the first comment. (Here you can specify a set of default parameters.) - In the
param block, decorate the parameters with the Parameter attribute to indicate the required and the set of parameters.
In this way:
[CmdletBinding]
param (
[parameter (mandatory = $ true, ParameterSetName = 'lines')
[int] $ Lines,
[parameter (mandatory = $ true, ParameterSetName = 'chars')
[int] $ | Chars
)
To access the used parameter set $PSCmdlet , which provides access to the same information available in cmdlets written in C # or VB.
source share