2

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

Mike
  • 21
  • 2
  • `${ Chrome File Path and Version }` this isn't a splat. What is this supposed to be? – Doug Maurer Aug 17 '20 at 05:16
  • @DougMaurer That is how you create a variable with spaces or special characters, E.G. Hyphens. – Drew Aug 17 '20 at 06:02
  • 1
    I think you are right, this way is simply not supported. I could not find any official docs/specs though. Using "simple name" variables is the only way to splat. – Roman Kuzmin Aug 17 '20 at 06:45

3 Answers3

2

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

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
0

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

RoXTar
  • 164
  • 1
  • 13
0

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.

Dabombber
  • 436
  • 3
  • 8
  • I'm curious what makes it a horrible idea? Other than the inconvenience of typing it out, what other problems besides the inability to directly reference it when splatting might one come across? – Mike Aug 19 '20 at 21:10
  • I'm not sure if there's any other technical limitations, but they are explicitly mentioned as something to avoid in the [PowerShell documentation](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_variables#variable-names-that-include-special-characters). – Dabombber Aug 20 '20 at 08:10