schuelermine
schuelermine

Reputation: 2290

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

Upvotes: 9

Views: 934

Answers (1)

mklement0
mklement0

Reputation: 437080

While it is technically possible to use a hashtable 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 hashtable-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.

Upvotes: 10

Related Questions