why doesn't splat work directly with command in powershell?

886 views Asked by At

Why does the following work:

Get-WmiObject -class Win32_OperatingSystem

And this works also:

$params = @{class='Win32_OperatingSystem'}
Get-WmiObject @params

But this doesn't work:

Get-WmiObject @{class='Win32_OperatingSystem'}

ERROR:

Get-WmiObject : Invalid query "select * from System.Collections.Hashtable"
At line:1 char:1
+ Get-WmiObject @{class='Win32_OperatingSystem'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
1

There are 1 answers

0
Sage Pourpre On

Splatting is a method of passing a collection of parameter values to a command as unit. Windows PowerShell associates each value in the collection with a command parameter. Splatted parameter values are stored in named splatting variables, which look like standard variables, but begin with an At symbol (@) instead of a dollar sign ($). The At symbol tells Windows PowerShell that you are passing a collection of values, instead of a single value.

If you do not store it into a variable, it is not splatting, it is a simple hashtable, which is passed as a positional argument into the command.

Reference

About splatting