When to use Quotes in PowerShell Splatting

Because I believe you should be using Splats anyway

Words: 449

Time to read: ~ 2 minutes

Rob Sewell ( blog | twitter ) did a post about PowerShell Splatting. I like Splatting; it’s formatted, it’s reusable, and it can be customised. So when a question came up about Splatting I figured it would be a good way to check that I’ve got a handle on the topic.

If you’ve checked the examples in that post – and I recomment that you do – then you’ll see that it takes the syntax of Parameter = 'Value'.

Notice the Parameter portion is not in quotes? It also works perfectly well if you have the Parameter name in quotes e.g. 'Parameter' = 'Value' (double quotes works too).

Our Question is Why?

Why would you use one instead of the other?

You’d use quotes if there is a space or a keyword in the parameter name.

Let’s take an example of a custom function with a parameter that has a space in its name. A nice simple one that just spits out what we pass into it.

Passing in the value of “Stupid PC” we can see that it accepts it.

ConvertTo-StupidParam 'Stupid PC'

OriginalExample
I’ll leave it to you to figure out why I didn’t use -computer

Splat with No Quotes

So we have a cmdlet with a parameter with spaces. I’ve said that this won’t work but let’s prove that. Try and create a Splat of a parameter with spaces without using quotes. I couldn’t!

NoQuotesAttempt
Splats are hash tables in the end…

Now, we can get around this…

We happen to know, thanks to Thomas Raynor ( blog | twitter ), that PowerShell supports partial parameter names as long as they cannot be confused with any other parameters.


$noquotes = @{ computer = 'Stupid PC' }
ConvertTo-StupidParam @noquotes

PartialParameterName

That’s not going to work if we have two parameters that we can’t uniquely identify though, say -computer source and -computer destination. To deal with these, we’ll need to use quotes in our Splats.

Splats with Quotes

Now let’s make a Splat with quotes and see if that will work for us?


$quotes = @{ 'computer name' = 'Stupid PC' }
ConvertTo-StupidParam @quotes

SplatWithQuotes
Splatty-Splatty

When we use a Splat with quotes, the space in the parameter name is no longer a problem for us. We are no longer confined to the realm of single word parameters.

Before is a world filled with parameters and variables wrapped in squiggly brackets “{ }”…hmm, not sure if this is a good thing…

Final Got’cha

So there you go! You should now know when you need to use quotes in Splats and when you don’t. Whether or not you like using quotes is a separate thing altogether; whatever way your style works.

At least you know when you have to now. 🙂

 

 

Author: Shane O'Neill

DBA, T-SQL and PowerShell admirer, Food, Coffee, Whiskey (not necessarily in that order)...

One thought on “When to use Quotes in PowerShell Splatting”

What's your opinion?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s