powershell splatting

Splatting in PowerShell makes code easier to read. Instead of typing a bunch of parameters allllll across the screen, you can use an easy-to-read hashtable or array. Argument splatting was introduced in PowerShell v3 and works with all PowerShell commands, not just dbatools.

Note: I’ve only used splatting with hashtables, as they allow me to be explicit about which parameters I’m passing. It appears that arrays would employ positional parameters, which is less wordy but leaves room for error.

Example 1

In the example below, sql2008 will be migrated to sql2017 using the backup restore method.

Note that the dollar sign in the variable name $splat becomes an at sign, @splat when executing the command. Also, not every parameter has to belong to the splat. As you can see, -Force is specified separately.

Also, the use of quotes is slightly different. While quotes are sometimes optional (string variables without special characters) when executing a command the traditional way, quotes around string values are required in a hashtable.

Example 2

The first example was a short intro and the next ones are a bit longer. Imagine the following splat used with one of our larger commands, Invoke-DbaDbLogShipping.

You can see above that switches such as -Force or -CompressBackup when used in a splat will use the following syntax Force = $true.

Example 3

Here’s another example we use in our Extended Events presentation.

Example 4

And finally, an example that will be included in the High Availability blog post when it finally comes out.

VS Code

In his blog post Easily Splatting PowerShell with VS Code, Rob Sewell shows how VS Code can easily create splats, as explained in his instructions and video below. First, run:

then:

Write the command, leave the cursor on a parameter, hit F1 – Choose PowerShell : Show Additional Commands (or use a keyboard shortcut) type splat press enter. Done πŸ™‚

Rob also tweeted to “be careful where your cursor is. I usually put it in the command. Sometimes it goes screwy if the cursor is at the EoL.”

Have fun

If you use commands with a number of parameters or even want to reuse parameter sets, splatting is an ideal solution.

- Chrissy

8 thoughts on “powershell splatting

  1. Pingback: Splatting In Powershell – Curated SQL

  2. Simon Reply

    I’ve always used the backtick symbol ( ` )

    I can see the benefit of splatting, will likely adopt. Have you used backtick before? Have an opinion on it?

    Cheers

    • Chrissy LeMaire Post authorReply

      I have used it and even know a bit of trivia about it! The backtick works because it escapes the next character which is a line break πŸ§™β€β™‚οΈ But I don’t like the way it looks so I’ve only used it a couple times.

  3. Micha Piatek Reply

    I just found this site the other day reading a blog post by Kendra Little and I am working with dbatools only since this week Great article, great tools. I am amazed and excited. Great job. This is the PS on SQL know how library I have been looking for.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.