1

Suppose I write a simple script that takes $name as a cli parameter and print it. I know that when I invoke the script it can autocomplete -name, but is it possible to give PowerShell a list of values from which to autocomplete the value assigned to $name?

1 Answer 1

6

You want the [ValidateSet()] feature for your parameter. It is used as such:

Function Test-Me{
Param(
    [ValidateSet("Matt","John","Andrew")][String]$Name
)
    "You designated $Name"
}

Then when you access the function and go to fill in the -Name argument it will pop out a list of applicable names from the list provided.

Pop-Out List of Names

Sign up to request clarification or add additional context in comments.

2 Comments

That's great, thanks! Little problem: only works on 8.1, not on server 2012 R2. Any idea why? thanks anyway
Sorry, I missed your comment until just now. I'm afraid that I do not know why it doesn't work on Server 2012 R2, I do not have a server available to me. Are you using the ISE in both cases? I think tab-completion should still work either way. Sorry, I can't really test on server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.