Splatting variable that start with a space

280 views Asked by At

Is there a way to continue using the name that begins with a space and splat that?

So this of course works:

$Splat = @{
name = 'chrome'
fileversioninfo = $true
}
(Get-Process @Splat)[0]

For me it returns:

ProductVersion   FileVersion      FileName
--------------   -----------      --------
84.0.4147.125    84.0.4147.125    C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

Now, if I change the variable name to ' Chrome File Path and Version ' I get this:

${ Chrome File Path and Version } = @{
name = 'chrome'
fileversioninfo = $true
}

The variable is valid and returns the properties:

Name                           Value
----                           -----
fileversioninfo                True
name                           chrome

But trying to splat it, it thinks I am trying to create a hashtable because of the brackets:

At line:5 char:23
+ (Get-Process @{ Chrome File Path and Version })[0]
+                       ~
Missing '=' operator after key in hash literal.

So my question is, anyone aware of a way to splat a variable that requires brackets around it? I'm well aware a simple

$splat = ${ Chrome File Path and Version }

Would work, but the question isn't for a workaround, just if there's a way to splat variables with a space as the first character. I've tried escape characters, single/double quotes, subexpressions and piping to drop the name in place but with no documentation on this, I'm pretty sure it's just not supported :/

Also, in case it matters, I am still on version 5.1.19041.1

3

There are 3 answers

0
RoXTar On

This is like you wish to have an Variablename like this: variable with space = 1; in other languages. This is not supported

0
JPBlanc On

Splatting is a method of passing a collection of parameter values to a command as a unit, it's supposed to make your commands shorter and easier to read.

Ok PowerShell provides a way to use special caracters in a variable name, but I don't Think it's a good practice, you should know it exists, you should not use it.

The simple fact it uses brakets interfer with the initialisation of hashtables in splatting. So as @Roman Kuzmin propose it's simply not supported

2
Dabombber On

Horribleness of the idea aside, splatting in PowerShell is currently quite limited, so an intermediate variable is the best way. There's a RFC draft which would improve splatting here, but its implementation isn't currently planned.