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 Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

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!



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!

Join the 4sysops PowerShell group!


Users who have LIKED this post:

  • avatar
  • avatar
  • avatar
  1. Rob Jaudon 3 years ago


    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.



  2. Author
    Adam Bertram 3 years 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'


    Users who have LIKED this comment:

    • avatar
  3. Rob Jaudon 3 years ago


    Thank you...this helped.



Leave a reply

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


© 4sysops 2006 - 2019


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


Log in with your credentials


Forgot your details?

Create Account