How to specify a non-positional parameter in a PowerShell script?

Question:

I’ve got the following param block at the start of a script

This is fine, only I need $version to not be a positional parameter, so that if you type

Then $args should contain “argument” and $version should be “1.1.0”. I know I can do it with a C# Cmdlet, but it would be more convenient if I could deliver this as a single script.

Answer:

In PowerShell 1.0, not possible AFAIK. You would need to remove the $version parameter if you don’t want it to be positional.

In PowerShell 2.0, you can get what you want by creating an advanced function in which you specify additional info in a Parameter attribute e.g.:

Source:

How to specify a non-positional parameter in a PowerShell script? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply