title: How do I specifiy the param() section in a function so that I can call the function with either "F -opt val" or "F val_1 val_2"?
body: <p>I am trying to write a PowerShell function that I can invoke using a switch and passing <em>one</em> value or not using the switch and passing <em>two</em> values:</p>
  <pre><code>option-or-value  -opt  one
  option-or-value        two  three
  </code></pre>
  <p>I have assumed that I need parameter sets and need to define the <code>param()</code> section of the function as follows:</p>
  <pre><code>function option-or-value {
     param (
  
        [parameter (
            parameterSetName = 'using_option',
            mandatory        =  $true
         )][switch           ]                 $opt,
  
        [parameter (
            parameterSetName = 'using_value',
            mandatory        =  $true
         )][string           ]                 $val_0,
  
        [parameter (
            mandatory        =  $true
         )][string           ]                 $val_1
     ) 
  
     if ($using_option) {
         $val_0 = '...'
     }
  
     write-host &quot;val_0 = $val_0, val_1 = $val_1&quot;
  }
  </code></pre>
  <p>However, I am doing something wrong and when I try to invoke the function, I get the error <em>A positional parameter cannot be found that accepts argument</em>.</p>
  <p>This surprises me because I thought the entire point of parameter sets is that functions can differentiate between different parameter protocols.</p>
  <p>So, where is my assumption wrong and how can I achieve such a function?</p>