title: What is the proper way to define a dynamic ValidateSet in a PowerShell script?
body: <p>I have a PowerShell 7.1 helper script that I use to copy projects from subversion to my local device. I'd like to make this script easier for me to use by enabling PowerShell to auto-complete parameters into this script. After some research, it looks like I can implement an interface to provide valid parameters via a ValidateSet.</p>
  <p>Based on <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.1#dynamic-validateset-values-using-classes" rel="nofollow noreferrer">Microsoft's documentation</a>, I attempted to do this like so:</p>
  <pre><code>[CmdletBinding()]
  param (
      [Parameter(Mandatory)]
      [ValidateSet([ProjectNames])]
      [String]
      $ProjectName,
  
      #Other params
  )
  
  Class ProjectNames : System.Management.Automation.IValidateSetValuesGenerator {
      [string[]] GetValidValues() {
          # logic to return projects here.
      }
  }
  </code></pre>
  <p>When I run this, it does not auto-complete and I get the following error:</p>
  <pre><code>❯ Copy-ProjectFromSubversion.ps1 my-project
  InvalidOperation: C:\OneDrive\Powershell-Scripts\Copy-ProjectFromSubversion.ps1:4
  Line |
     4 |      [ValidateSet([ProjectNames])]
       |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       | Unable to find type [ProjectNames].
  </code></pre>
  <p>This makes sense since the class isn't defined until after the parameters. So I moved the class above the parameters. Obviously this is a syntax error. So how do I do this? Is it not possible in a simple PowerShell script?</p>