Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 882
Walter Avatar asked Dec 08 '25 19:12

Walter


1 Answers

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

like image 198
Sage Pourpre Avatar answered Dec 10 '25 11:12

Sage Pourpre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!