title: PowerShell function arguments: Can the first one be optional first?
body: <p>I have an advanced function in PowerShell, which roughly looks like this:</p>
  
  <pre><code>function Foo {
      [CmdletBinding]
      param (
          [int] $a = 42,
          [int] $b
      )
  }
  </code></pre>
  
  <p>The idea is that it can be run with either two, one or no arguments. However, the first argument to become optional is the <em>first</em> one. So the following scenarios are possible to run the function:</p>
  
  <pre><code>Foo a b    # the normal case, both a and b are defined
  Foo b      # a is omitted
  Foo        # both a and b are omitted
  </code></pre>
  
  <p>However, normally PowerShell tries to fit the single argument into <em>a</em>. So I thought about specifying the argument positions explicitly, where <em>a</em> would have position 0 and <em>b</em> position 1. However, to allow for only <em>b</em> specified I tried putting <em>a</em> into a parameter set. But then <em>b</em> would need a different position depending on the currently-used parameter set.</p>
  
  <p>Any ideas how to solve this properly? I'd like to retain the parameter names (which aren't <em>a</em> and <em>b</em> actually), so using <code>$args</code> is probably a last resort. I probably could define two parameter sets, one with two mandatory parameters and one with a single optional one, but I guess the parameter names have to be different in that case, then, right?</p>