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

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.

Do-Something -ParameterName ParameterArgument

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?

Do-Something -ParameterName ParameterArgument -Thing SomeArgument -DooHickey SomeOtherArgument -Widget WidgetNameThing -OutOfIdeas 'foo bar'

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:

$params = @{
    ParameterName = 'ParameterArgument'
    Thing = 'SomeArgument'
    DooHickey = 'SomeOtherArgument'
    Widget = 'WidgetNameThing'
    OutOfIdeas = 'foo bar'
}

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:

Do-Something @params

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.

Subscribe to 4sysops newsletter!

Do-Something @params -ThisWorks 'Args1' -ThisWillWorkToo 'Args2'

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

avataravataravatar
5 Comments
  1. Rob Jaudon 6 years 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

  2. Author
    Adam Bertram (Rank 3) 6 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’

    avatar
  3. Rob Jaudon 6 years ago

    Adam,

    Thank you…this helped.

    Rob

  4. Brandon Bates 3 years ago

    Hey Adam,

    I am curious if you know of a way to use splatting when you don't want to predeclare the hashtable in a variable. Most of the time I am pulling data in from other objects to create the hashtable and don't want to have an extra variable that I will only use once to pass into a function.

    Here is an example of what I am talking about,

    function foo {

        param(

            $Name,

            $Position,

            $DepartmentName

        )

        "$Name is an $Position working in the $DepartmentName department"

    }

     

    # Works, But Creates an Extra Variable Called $fooParams

    $fooParams = @{

        Name = "John"

        Position = "Accountant 1"

        DepartmentName = "Accounting"

    }

     

    foo @fooParams

     

    # Doesn't Work

     

    foo @{

        Name = "John"

        Position = "Accountant 1"

        DepartmentName = "Accounting"

    }

     

    # Also Doesn't Work

    @{

        Name = "John"

        Position = "Accountant 1"

        DepartmentName = "Accounting"

    } | foo

     

    Thanks,

    Brandon

    • I am not proud of this, but I did make a work around..

      function SmartSplat {

          param(

              [string]

              $FunctionName,

       

              [hashtable]

              $Params

          )

          & $FunctionName @Params

      }

       

      SmartSplat foo @{

          Name = "John"

          Position = "Accountant 1"

          DepartmentName = "Accounting"

      }

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account