title: Getting selected value of a parameter to provide values for next parameter in Powershell
body: <p>I am writing a function where I am planning to provide dynamic parameter auto-completion. Idea is this;</p>
  
  <p>When I type</p>
  
  <p><code>Activate -User &lt;USERNAME&gt;</code> (<code>-User</code> gets auto-completed by <code>GetUsers</code> class)</p>
  
  <p>I wanna use <code>&lt;USERNAME&gt;</code> selected by <code>-User</code> parameter in <code>GetValidValues</code> method of <code>GetProfiles</code> class so I can dynamically determine what user directory I should be searching profile files in, since every user would have different profile files path.</p>
  
  <p>I haven't been able to find a mechanism where I would get any selected value of any parameters. I am using PS6 and I was wondering if there is a way to do this. Here is my minimal code.</p>
  
  <pre><code>class GetUsers : System.Management.Automation.IValidateSetValuesGenerator
  {
      [String[]] GetValidValues()
      {
          # I simply return the user array here, no big deal
          return [string[]] $usersArray
      }
  }
  class GetProfiles : System.Management.Automation.IValidateSetValuesGenerator
  {
      [String[]] GetValidValues()
      {
          # Here I need to get what value is selected for $User parameter
          # of the function below, so I can populate $profileArray for selected
          # user
          return [string[]] $profileArray
      }
  }
  function global:Activate()
  {
      [CmdletBinding()]
      Param
      (
          [ValidateSet([GetUsers])]
          [string]$User="",
          [ValidateSet([GetProfiles])]
          [string]$Profile="",
      )
      # do business as usual
      Write-Host $User
      Write-Host $Profile
  }
  </code></pre>
  
  <p>Thanks!</p>