powershellparameter-splatting

Does PowerShell splatting support inline hashtable?


I want to use splatting syntax, but I want to do it inline

So instead of:

$p = @{Path = '.'}
ls @p

I want to do

ls @@{Path = '.'}

but this is a syntax error. Is there anyway to splat a hashtable without having to write a separate variable?

Why do I want to do this? I prefer the hashtable syntax for defining alot of parameters (like 4 or more). But I'd rather not define a variable, I just want to have the cmdlet I'm calling at the "top" of the hashtable definition.


Solution

  • The splat operator will look for a variable whose name matches the characters after the splat sign, it doesn't try to resolve the characters after the sign as a statement to get the value. You cant do it inline. Link to TechNet.