Powershell – What is Splatting ?

What is Splatting ?

Splatting is a method of passing a collection of parameter values to a command as unit.
It can make your commands shorter and easier to read, especially if they are long…
Really all it is, is just a way to pass parameters to commands, typically with a hash table https://technet.microsoft.com/en-us/library/ee692803.aspx


You could use this powershell cmdlet which has lots of parameters

Register-ScheduledTask -TaskName "ArchiveLogFiles" -Action $action -Trigger $trigger -User $Username -Password $Password -Settings $settings -RunLevel "Highest" -Description "Archives Log files"

You could use back ticks “`” to make it more readable, but that looks ugly and prone to error (forgetting to use the back tick causes error in your code)

Register-ScheduledTask `
-TaskName "ArchiveLogFiles" `
-Action $action`
-Trigger $trigger `
-User $Username`
-Password $Password`
-Settings $settings`
-RunLevel "Highest"`
-Description "Archives Log files"

So … Splatting , you do this

$params = @{
"TaskName"    = "ArchiveLogFiles"
"Action"      = $action
"Trigger"     = $trigger
"User"        = $Username
"Password"    = $Password
"Settings"    = $settings
"RunLevel"    = "Highest"
"Description" =  "Archives Log files"

Register-ScheduledTask @Params

So, all you are doing is creating a hash table of all the parameters you want to use and then using the @ to splat those parameters in after the cmdlet name

Looks like the back tick option, but…. tidier and easier to read
Further reading:


Leave a comment

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.