Splatting in PowerShell allows you to bundle parameters using a hash table when executing commands. This can save you a lot of typing.

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.

PowerShell allows a developer to perform the same task in many different ways. This is nice, because it doesn't force scripters into using one particular way. Depending on the circumstances, one way that works great for one person may not be feasible for another. However, this flexibility can lead to less than great code. Those unfamiliar with good coding practices can accomplish tasks, but at the cost of ease of management and future code expansion. One of those small practices is parameter declaration.

Passing parameters to PowerShell functions typically entails typing a hyphen followed by the parameter name and then the parameter argument.

This approach works great if you only have a few parameters to pass. Using the example above, what does this function call look like with five parameters?

Putting the current article formatting aside, that's 140 characters long! Here's what that would look like on a 1280 x 720 resolution screen in the PowerShell ISE. Notice the scrollbar? We don't want that. That means we're going to have to scroll back and forth just to see this function call.

So we need to fix this.

Long function call

Long function call

One way to fix this is to use backticks. Backticks are a way to tell PowerShell that you're not done yet. It's a way to spread out what you could define on a single line onto multiple lines. In our case of setting parameters, our command could look like the example below.

Using the backtick

Using the backtick

This method looks better and removes that scroll bar, but it's not perfect. Why? Notice how little those backticks are? Imagine it's 3 a.m. on a Tuesday night, and you're struggling to stay awake. How easy will you be able to see those backticks? I propose another way called splatting.

Splatting is a way to define function parameters ahead of time in a hash table variable. You would then use them one time or share them among many different functions if necessary. To use splatting, you'd first define a hash table with a key representing each parameter. Our example above would now look like this:

Now that we've defined each parameter in a hash table, we can pass this entire hash table as one unit to our function. However, instead of passing it as a variable with the leading $ that you're used to, you'd now use the @ symbol to tell the function that the hash table will provide all variable definitions.

The function call would now look like this:

Notice how much shorter it makes the function call? To me, this is a much more succinct way of passing parameters. You can now see from the screenshot below that each parameter is tightly defined in the hash table with appropriate syntax highlighting in the PowerShell ISE, all while extending up to only 40 characters wide!

Splatting

Splatting

This is also an excellent way to prevent parameter duplication as well by defining a standard set of parameters to use across many different functions. Splatting also allows you to use the standard hyphen notation as well. This means that even if the parameters aren't identical across multiple functions, you can still use them as a shared parameter set.

The next time you find yourself scrolling back and forth to look at a function call too long, remember to use splatting!

Win the monthly 4sysops member prize for IT pros

Share
10+

Users who have LIKED this post:

  • avatar
  • avatar
  • avatar

Related Posts

3 Comments
  1. Rob Jaudon 8 months ago

    Adam,

    Thanks for the post.  I can understand how this could be useful.  One request, could you give a real world example of how you've used splatting to help me understand it better.

    Not trying to be rude with the request...I just would like to see an example to help me understand it better.

    Thank you again for the post.

    Rob

    1+

  2. Author
    Adam Bertram 8 months ago

    Sure. Other than the obvious reason to limit the line length, I use splatting a lot to share parameters between function calls. For example, if you have two functions that different parameters but have some parameters that are the same, rather than, defining the parameters twice, they can share them.

    $params = @{
    Name = 'FOO'
    Host = 'BAR'
    }

    Get-VM @params
    Set-VM @params -Attribute 'Thing'

    2+

    Users who have LIKED this comment:

    • avatar
  3. Rob Jaudon 8 months ago

    Adam,

    Thank you...this helped.

    Rob

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account