PowerShell Splatting: Overview

Have you ever been writing a PowerShell script, and looked at how wordy and long the command becomes? If so, you’ll want to familiarize yourself with the concept of PowerShell Splatting. This technique allows you to declare your PowerShell cmdlet parameters in a HashTable (aka. dictionary or key-value pairs), and then “splat” those parameters onto a cmdlet invocation. There are a couple of benefits to using this technique in your scripts:

  • Your code will become more readable
  • You have the opportunity to audit your parameter values prior to invoking a command

Check out the YouTube video below to see an example of how to use PowerShell Splatting!

Generate PowerShell Splatting HashTables

Thanks to a brilliant idea from @embeducation (Microsoft MVP Ilija Injac) on Twitter, I authored a PowerShell function that allows you to generate PowerShell Splatting HashTables, instead of having to type them out by hand. You can generate Splatting HashTables for all of the commands in your PowerShell session (for each parameter set), or you can use the -CommandName parameter to specify an array of command names that you want to generate the HashTables for. You can also optionally specify an -OutputPath parameter, to output the generated HashTables to a user-defined location of your choice. 🙂 Even better, there is an -Interactive switch parameter that allows you to select the individual parameters that you want to be generated, instead of the command outputting all parameters (including PowerShell “common parameters”), which is the default behavior.