r/PowerShell • • Apr 05 '19

PowerShell tricks: Splatting

https://blog.destruktive.one/powershell-tricks-splatting/
150 Upvotes

58 comments sorted by

View all comments

29

u/a-rich Apr 05 '19

Taking it one step further, you can pass multiple splats.

Say, for instance you're using Send-MailMessage and have some common parameters, like smtpserver, from and port, you can put them all into $CommonSplat.

Then you can make a separate splat for more dynamic parameters, like To, Subject and Body as $DynamicSplat and pass both.

You can build that dynamic splat based on certain conditionals and use one send-mailmessage to take care of the sending all nice and neat like.

Send-MailMessage @CommonSplat @DynamicSplat

And since the splat is just a hash table, you can build it and customize it however you like.

$Splat.add()

$Splat.remove(

$Splat[key] = @("An array", "of values", "if the parameter", "supports it")

That article is ok as a very brief into to splatting, but theres definitely more to explore!

4

u/[deleted] Apr 05 '19

I feel like an idiot for not knowing this. I usually write if statements to dynamically build my splat didn't realize you use multiple ones, but I feel like I should have.