'Can I use splatting to pass arguments to a non-powershell executable?

Can I do something like this?

$splatting_table = @{
   "-parameter" = "value"
   "-parameter2" = "value2"
}
.\external-command.exe @splatting_table

as an equivalent for

.\external-command.exe -parameter value -parameter2 value2


Solution 1:[1]

While it is technically possible to use a hash table for splatting with external programs, it will rarely work as intended.[1]

Instead, use an array:

$splatting_array = 
   '-parameter', 'value',
   '-parameter2', 'value2'

.\external-command.exe @splatting_array

Note that $splatting_array is simply a flat array - formatted for readability in element pairs - whose elements PowerShell passes as individual arguments.


[1] With hash table-based splatting, do not include the - sigil in the key names (e.g., use parameter, not
-parameter); aside from that, PowerShell will join your entries with a : when constructing the command line for the external program, which few programs support; e.g., hash-table entry parameter = 'value' translates to
-parameter:value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1