Get ValueFromRemainingArguments as an hashtable

Question:

Using [parameter(ValueFromRemainingArguments=$true)] one can get all the remaining arguments passed to the function into a variable as a list.

How can I get the remaining arguments as a hashtable type, for example for inputs like Function -var1 value1 -var2 value2?

Answer:

There are multiple ways to achieve this. The following solution supports parameters with:

  • Simple value (single item)
  • Array value
  • Null value (switch)

Script:

Output:

Edit: Modified default value assigned to Hashtable keys: $htvars[$lastvar] = $true. Using $true as the default accounts for switch parameters and can make the resulting Hastable more “splattable”.

Source:

Get ValueFromRemainingArguments as an hashtable by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply