Search notes:

Powershell: Param(Parameter(ParameterSetName=…))

The following script converts either from cartesian coordinates to polar coordinates, or vice versa.
Because the script can be invoked in different «modes», it operates with two parameter sets, named cartesianCoordinates and polarCoordinates.
The cmdletBinding(defaultParameterSetName… defines the default mode (in this case it is conversion from cartesian coordinates to polar coordinates).
When the script is executed, it uses System.Management.Automation.PSCmdlet - ParameterSetName (exposed through the $psCmdLet automatic variable) to determine which parameter set is in effect.
Using the script in default mode:
PS> .\convert-coordinates.ps1 8.2 3.1
Cartesian to polar: r = 8.76641317757725, phi = 1.20935543590134
In order to convert from poloar coordinates to cartesian coordinates, the non-default parameter set polarCoordinates is needed. This can be achieved by explicitly specifying (at least) one parameter name of a non-default parameter set:
PS> .\convert-coordinates.ps1 -r 7.8 -phi 3.4
Polar to cartesian: x = -7.5410259021198, y = -1.99322059580928
Conflicting parameter names:
PS> .\convert-coordinates.ps1 -r 6.6 -x 8.8
… Parameter set cannot be resolved using the specified named parameters. …
[cmdletBinding( defaultParameterSetName = 'cartesianCoordinates' )]

param(

    [ parameter(parameterSetName = 'cartesianCoordinates', mandatory = $true, position=0 )] [double] $x,
    [ parameter(parameterSetName = 'cartesianCoordinates', mandatory = $true, position=1 )] [double] $y,
      
    [ parameter(parameterSetName = 'polarCoordinates'    , mandatory = $true, position=0 )] [double] $r,
    [ parameter(parameterSetName = 'polarCoordinates'    , mandatory = $true, position=1 )] [double] $phi
)

set-strictMode -version 3

if ($psCmdlet.parameterSetName -eq 'cartesianCoordinates') {

   $r   = [math]::sqrt($x*$x + $y*$y)
   $phi = [math]::atan($x / $y)

  "Cartesian to polar: r = $r, phi = $phi"
}
else {
   $x = $r * [math]::cos($phi)
   $y = $r * [math]::sin($phi)

  "Polar to cartesian: x = $x, y = $y"
}

See also

Parameter sets

Index