title: Powershell: C#-style function overloads with Parameter Sets?
body: <p>In C#, function overloading has historically appeared something like the following, where each overload adds some amount of parameters on top of the simpler signatures:</p>
  
  <pre><code>public void Initialize(int version);
  public void Initialize(int version, string workspaceName);
  public void Initialize(int version, string workspaceName, Path workspaceRoot, bool force);
  </code></pre>
  
  <p>In Powershell, overloads such as these are not possible; however, some approximation of function overloading is offered through System.Management.Automation.ParameterAttribute's ParameterSetName property. This allows us to declare parameters as members of certain parameter sets, which allows us to effectively define separate signatures for our functions. The simplistic case found in most <a href="http://blogs.msdn.com/b/powershell/archive/2008/12/23/powershell-v2-parametersets.aspx" rel="nofollow">ParameterSetName examples</a> is something like this:</p>
  
  <pre><code>function test-param 
  { 
  [CmdletBinding(DefaultParametersetName="p2")] 
  param( 
  [Parameter(ParameterSetName="p1",Position=0)] 
  [String] 
  $d, 
  
  [Parameter(ParameterSetName="p2", Position=0)] 
  [String]$i 
  ) 
      switch ($PsCmdlet.ParameterSetName) 
  { 
      "p1"  { Write-Host ([DateTime]$d); break} 
      "p2"  { Write-Host ([INT]$i); break} 
      } 
  }
  </code></pre>
  
  <p>Get-help on this function yields the following:</p>
  
  <pre><code>PS D:\.ws&gt; get-help test-param
  test-param [[-i] &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  test-param [[-d] &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  </code></pre>
  
  <p>This basic type of example isn't incredibly for complex usages of parameter sets, however, because the parameter sets are entirely mutually exclusive. But what if we want some parameters to be members of all parameter sets? More experienced users might suggest that this is a case for the special "AllParameterSets" parameter set, which according to MSDN is the default if a parameter set is not specified. However, consider the following:</p>
  
  <pre><code>PS D:\.ws&gt;     function test-param 
  { 
      [CmdletBinding(DefaultParametersetName="p2")] 
      param
      ( 
          [Parameter(ParameterSetName="p1",Position=0)] 
          [String] 
          $d, 
          [Parameter(ParameterSetName="p2", Position=0)] 
          [String]$i,
          [Parameter(ParameterSetName="AllParameterSets")] 
          [String]$x
      ) 
  }
  
  PS D:\.ws&gt; get-help test-param
  test-param [[-i] &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  test-param [[-d] &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  test-param [-x &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  </code></pre>
  
  <p>Apparently when "AllParameterSets" is specified as the parameter set, it is in fact not included in all parameter sets as one might expect. This can be seen by omitting the declaration:</p>
  
  <pre><code>PS D:\.ws&gt;     function test-param 
  { 
      [CmdletBinding(DefaultParametersetName="p2")] 
      param
      ( 
          [Parameter(ParameterSetName="p1",Position=0)] 
          [String] 
          $d, 
          [Parameter(ParameterSetName="p2", Position=0)] 
          [String]$i,
          [String]$x
      ) 
  }
  
  PS D:\.ws&gt; get-help test-param
  test-param [[-i] &lt;String&gt;] [-x &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  test-param [[-d] &lt;String&gt;] [-x &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  </code></pre>
  
  <p>Now, the $x parameter appears in all function signatures like we'd expect.</p>
  
  <p>Finally, the most complex case. What if we want one parameter to appear in some, but not all signatures? The workspaceName parameter given in the original example fits this description. Perhaps a key to this solution is realizing that multiple Parameter properties can be declared for each parameter in the param block. This allows us to establish a parameter set for each signature, and decorate each parameter with a Parameter attribute for each set it belongs to. Consider the following:</p>
  
  <pre><code>PS D:\.ws&gt; function Initialize-Something
  {
      [CmdletBinding()]
      param
      (
          [Parameter(ParameterSetName="version")]
          [Parameter(ParameterSetName="workspaceName")]
          [Parameter(ParameterSetName="createWorkspace")]
          [int] $Version,
          [Parameter(ParameterSetName="workspaceName")]
          [Parameter(ParameterSetName="createWorkspace")]
          [string] $WorkspaceName,
          [Parameter(ParameterSetName="createWorkspace")]
          [string] $WorkspaceRoot,
          [Parameter(ParameterSetName="createWorkspace")]
          [switch] $Force
      )
  }
  
  PS D:\.ws&gt; get-help initialize-something
  Initialize-Something [-Version &lt;Int32&gt;] [-WorkspaceName &lt;String&gt;] [-WorkspaceRoot &lt;String&gt;] [-Force] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  Initialize-Something [-Version &lt;Int32&gt;] [-WorkspaceName &lt;String&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  Initialize-Something [-Version &lt;Int32&gt;] [-Verbose] [-Debug] [-ErrorAction &lt;ActionPreference&gt;] [-WarningAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-WarningVariable &lt;String&gt;] [-OutVariable &lt;String&gt;] [-OutBuffer &lt;Int32&gt;]
  
  PS D:\.ws&gt; initialize-something -workspacename "test"
  Initialize-Something : Parameter set cannot be resolved using the specified named parameters.
  At line:1 char:21
  + initialize-something &lt;&lt;&lt;&lt;  -workspacename "test"
      + CategoryInfo          : InvalidArgument: (:) [Initialize-Something], ParameterBindingException
      + FullyQualifiedErrorId : AmbiguousParameterSet,Initialize-Something
  </code></pre>
  
  <p>Powershell claims it can't determine a parameter set, more than likely because WorkspaceName appears in multiple signatures. For the record, this also occurs when specifying only the -Version, or for that matter any signature other than the most complex. This can be alleviated somewhat with the [CmdletBinding(DefaultParameterSetName="version")] or something, but it's not a proper solution.</p>
  
  <p>So after all this, my question: How can I achieve the kind of signatures I'm looking for? Do I need to create a set of overly-explicit switches such as -VersionMode, -WorkspaceNameMode, -CreateWorkspaceMode to specify which mode I want it to run, essentially negating the niceness of parameter set detection? Perhaps a mode enum? Can this be done through some elegance using ParameterAttribute's Mandatory and Position properties?</p>
  
  <p>Thanks!</p>