title: Can I specify conditional default values for a parameter in PowerShell?
body: <p>I thought if this was possible it might work using parameter sets so I tried the following:</p>
  
  <pre><code>Function New-TestMultipleDefaultValues {
      [CmdletBinding(DefaultParameterSetName="Default1")]
      param (
          [Parameter(Mandatory,ParameterSetName="Default1")]$SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1,
          [Parameter(ParameterSetName="Default1")]$Test1 = "Value1",
          [Parameter(ParameterSetName="Default2")]$Test1 = "Value2"
      )
      $PSBoundParameters
  }
  </code></pre>
  
  <p>Executing this to create the function results in the error <code>Duplicate parameter $test1 in parameter list.</code> so it doesn't look like this way is an option.</p>
  
  <p>The only thing I can think of at this point is to do something like this:</p>
  
  <pre><code>Function New-TestMultipleDefaultValues {
      param (
          $SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1,
          $Test1
      )
      if (-not $Test1 -and $SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1) {
          $Test1 = "Value1"
      } elseif (-not $Test1 -and -not $SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1) {
          $Test1 = "Value2"
      }
  
      $Test1
  }
  </code></pre>
  
  <p>Which works but seems ugly:</p>
  
  <pre><code>PS C:\Users\user&gt; New-TestMultipleDefaultValues -SomeOtherThingThatIfSpecifiedShouldResultInTest1HavingValue1 "thing"
  Value1
  PS C:\Users\user&gt; New-TestMultipleDefaultValues
  Value2
  PS C:\Users\user&gt; New-TestMultipleDefaultValues -Test1 "test"
  test
  </code></pre>
  
  <p>Any better way to accomplish this?</p>