Learning To Love The Splat

As with all good scripting languages, there is more than one way to do things in PowerShell. The guidelines tend towards the conservative, encouraging you to eschew aliases, use full parameter names and use common idioms when writing scripts.

But hey, that’s no fun. And sometimes it’s downright verbose. And there’s only so much time in the day.

Besides, the shortcuts are there for a reason, right?

Right.

So on to splatting.

Ever had to call a cmdlet several times in a row, perhaps at the shell, perhaps in a script, or perhaps in a Pester test? Ever got sick of typing the parameters multiple times? Then splatting is for you.

Whut splatting?

Splatting refers to the technique of sticking your parameter values into a hashtable and throwing that – *splat* – at a Cmdlet or Function. Observe.

Thing to note: don’t pass $splat to the Cmdlet. Pass @splat.

This is not a new technique, by any means. It’s been around since PowerShell 2.0, but it’s only really become useful to me recently with our aggressive adoption of Pester at Domain Devops. This means I can bundle a set of parameters and throw them at Cmdlets repeatedly, to test different things. This is especially good in testing DSC resources, since I can throw the same set of parameters at Get-TargetResource, Test-TargetResource and Set-TargetResource over and over, with no chance of the params getting out of sync.

Powerful, fun technique. Sure, a bit confusing if you run into it without knowing what it is but now you do so there’s no excuse.

One reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">