Choose Parameter values for a list help

Product: PowerShell Universal
Version: 1.4.6

I am trying to have a parameter where the options are populated from a txt file and I am able to select one or more values.

I am not sure how to do this ValidateSet seems like a likely option except I cant figure out how to populate from a txt file or how to allow it to choose multiple selections.

Any help would be great!

Hi,

i dont know the exact usecase but here is a example to create a dynamic validateset for a function

$ValidateSet = (Get-Content -Path "Your\Path\ToFile\myTextFile.txt" | Foreach-Object { '"' + $_ + '"' }) -join ", "

$functionScriptBlock = @"
    function MyDynamicFunction {
        param(
            [ValidateSet($ValidateSet)]
            [string[]] `$MyParameter
        )

        # enter your code here for example
        # don't forget to escape '$'

        Write-Host `$MyParameter
    }
"@

# create the function in runtime
Invoke-Expression $functionScriptBlock

# use the created function
MyDynamicFunction -MyParameter "<Enter valid value here>"
MyDynamicFunction -MyParameter @("<Enter valid value here>", "<Entersecond valid value here">)

But I dont have a solution for now for your question to select more than one validateset

Edit:
Made the parameter for the validateset to an array so the function accepts one or more values for the parameter

Edit 2:
Typo

1 Like

Check out argument completions:

You can even manipulate the data to show specific parts of your data.

Here is a short example:

$Mydata = 'Apple','Banana','Cherry','Dragonfruit'

function Get-Fruit {
    param(
        [ArgumentCompleter({
             $Mydata |
                Where-Object { $_ -like "$wordToComplete*" }
        })]
        [string]$Name
    )

    "You selected: $Name"
}

Now if you type:

Get-Fruit -Name

and click ctrl+space, you can see the fruit options. You can then pick multiple by adding , and clicking ctrl+space again