title: Make parameter mandatory based on switch - PowerShell
body: <p>I was struggling in a situation where I need to make some parameters mandatory ONLY if a switch or combination of switches is used. Below is the example of what I am trying to do:</p>
  
  <pre><code>[CmdletBinding(DefaultParameterSetName='DefaultConfiguration')]
  Param
  (        
  [Parameter(Mandatory=$true)][String]$Location,
  [Parameter(Mandatory=$true)][String]$DPMServername,
  
  [Parameter(Mandatory=$False, ParameterSetName='CustomConfiguration')]
  [Switch]$CustomizeDPMSubscriptionSettings,
  [Parameter(Mandatory=$True, ParameterSetName='CustomConfiguration')]
  [String]$StagingAreaPath,
  
  [Parameter(Mandatory=$False, ParameterSetName='EncryptionSettings')]
  [Parameter(ParameterSetName='CustomConfiguration')]
  [Switch]$SetEncryption,
  [Parameter(Mandatory=$true, ParameterSetName='EncryptionSettings')]
  [Parameter(Mandatory=$False, ParameterSetName='CustomConfiguration')]
  [String]$EncryptionPassPhrase,
  
  [Parameter(Mandatory=$False, ParameterSetName='ProxyEnabled')]
  [Parameter(ParameterSetName='CustomConfiguration')]
  [Switch]$SetProxy,
  [Parameter(Mandatory=$true, ParameterSetName='ProxyEnabled')]
  [Parameter(ParameterSetName='CustomConfiguration')]
  [String]$ProxyServerAddress,
  [Parameter(Mandatory=$true, ParameterSetName='ProxyEnabled')]
  [Parameter(Mandatory=$False, ParameterSetName='CustomConfiguration')]
  [String]$ProxyServerPort
  )
  </code></pre>
  
  <p>Here, I need to follow below conditions:</p>
  
  <ol>
  <li>If -CustomizeDPMSubscriptionSettings (Switch) parameter is used,
  it must ask for the Staging AreaPath ---- This is Working fine</li>
  <li>ONLY when -CustomizeDPMSubscriptionSettings (Switch) parameter is
  used with -SetEncryption, it must ask for -EncryptionPassPhrase</li>
  <li>And ONLY when -CustomizeDPMSubscriptionSettings (Switch) parameter is
  used with -SetProxy, it must ask for -ProxyServerAddress and -ProxyServerPort</li>
  </ol>
  
  <p>Sorry if this sounds like a repeated question but other posts I found here are not helping me solve my issue. I am confused :-(</p>
  
  <p>NOTE: Above code is part of what I was trying with different combinations. Please correct as necessary.</p>