How To Use Parameter Splatting In PowerShell

Splatting. Who on the PowerShell team came up with that name anyway? Did they all have a meeting one day and Lee Holmes’ reheated Ziti falls off onto the floor and Bruce Payette has this epiphany to name a construct from the sound it makes? Probably.

Let's talk a little bit about splatting splatting is a powershell term that describes a method of bundling up parameters before sending them off to a commandlet or function. It's often used to increase the readability of a script. Take this usage of get some instance as an example. We have a number of parameters and values being supplied and this is resulted in the line length running longer than the width of my screen. If I run this, IT returns information about my C Drive. But in a script it's not very legible and we can use a hash table to help with this. A hashtable is a collection of key value peers and for splatting each key is the name of the parameter. For example, computer name or class name and the values are the values. We were supplying to each parameter. Once we have our completed hashtable, we can then supply that to the command using the at operator. This is important, and splatting won’t work without it as the at symbol tells powershell that we explain multiple values rather than a single value in in this situation you can consider the at symbol to be a splat operator. Now, if I run the seek an example. We get exactly the same output. As before information about my C Drive. However, in the script. This is much more legible and easy to follow but readability isn't the only reason to splat your parameters. It can also help you reduce mostly duplicated commandlet calls. This is an example from an advanced function that wraps around seemed mail message. It had default values for SMTP server from 2 and subject line. However, there were no default values for body or CC. To get around thus created a complicated if or structure. We're F body was present. It would be included in the send mail message core and likewise if CC was present it would be included in the cinnabar message core. This resulted in 4 different possible Sind Mal missed calls with largely the same parameters for each. This isn't very efficient or readable. And it also meant that have more variables were needed in the future that would drastically increase the complexity of this construct. However, you can use splatting to help reduce the duplicated commandlet calls and again, he start by creating a hashtable. Who even notice that the 4 keys in here? Are only the parameters that were present in all 4 of the previous command, locals and then for body in CC we apply some logic. So if body is present, we can then add that to the hash table. And likewise with CC we can add that to the hash table. Finally, once the hash tables all complete we can then supply that to send mail message again using the at symbol. Now the beauty of this is before finally sending it off to the Commandlet. We can manipulate that Hashtable as much as we like. And by manipulation, I mean, we can add values. Remove values or modify existing values right up until that point, Weiss called the commandlet once. The final thing to be aware of when doing splatting is that if you go directly to working with hash tables. You won't have any of the Intellisense or tab completion that you'd be accustomed to when using commandlets. So what you can do is work on your script as you normally would to build up your parameters. One thing to be aware of is this example here if I run. This is the command on a single line supplied file as shown here will work. However, in a hashtable. It won't because it will try to interpret this as a command rather than a valid item type value. So what you can do to speed up the creation of your hash table. This copy those parameters and paste them and. There's a hashtable complete and we finish this off by supplying that to new item. There we go, we have a new item created based on the search table. Thank you for watching.