title: PowerShell mandatory parameter depends on another parameter
body: <p>I have a PowerShell function which changes the registry key values. Code:</p>
  
  <pre><code>param(
      [Parameter()] [switch]$CreateNewChild,
      [Parameter(Mandatory=$true)] [string]$PropertyType
  )
  </code></pre>
  
  <p>It has a parameter, "CreateNewChild", and if that flag is set, the function will create the key property, even if it wasn't found. The parameter "PropertyType" must be mandatory, but only if "CreateNewChild" flag has been set.</p>
  
  <p>The question is, how do I make a parameter mandatory, but only if another parameter has been specified?</p>
  
  <p>OK, I've been playing around with it. And this does work:</p>
  
  <pre><code>param(
    [Parameter(ParameterSetName="one")]
    [switch]$DoNotCreateNewChild,
  
    [string]$KeyPath,
  
    [string]$Name,
  
    [string]$NewValue,
  
    [Parameter(ParameterSetName="two")]
    [switch]$CreateNewChild,
  
    [Parameter(ParameterSetName="two",Mandatory=$true)]
    [string]$PropertyType
  )
  </code></pre>
  
  <p>However, this means that $KeyPath, $Name and $NewValue are not mandatory any more. Setting "one" parameter set to mandatory breaks the code (<em>"parameter set cannot be resolved"</em> error). These parameter sets are confusing. I'm sure there is a way, but I can't figure out how to do it.</p>