Splatting is a method of passing a collection of parameter values to a command as unit. PowerShell associates each value in the collection with a command parameter. Splatted parameter values are stored in named splatting variables, which look like standard variables, but begin with an At symbol (@) instead of a dollar sign ($). The At symbol tells PowerShell that you are passing a collection of values, instead of a single value.

Splatting is the ability to use a dictionary or a list to supply parameters to a command. Proxy commands are wrappers of existing commands in Windows PowerShell, and to make this possible.Instead of having to stretch your parameter assignments out horizontally, Splatting gives us a clean, concise, readable format that extends vertically in which to assign values to our parameters.

PROS of Splatting:

  • Splatting makes your commands shorter and easier to read.
  • You can re-use the splatting values in different command calls and use splatting to pass parameter values from the $PSBoundParameters automatic variable to other scripts and functions.

SYNTAX

<CommandName> <optional parameters> @<HashTable> <optional parameters>
<CommandName> <optional parameters> @<Array> <optional parameters>

Example 1: Splatting with Array

In this examples compare two Copy-Item commands that copy the txt file to the txt file in the same directory. For better understanding,
first let we see the example uses the traditional format which we usually use all the time.

Copy-Item -Path ‘C:\blog\source.txt’ ‘C:\blog\desination.txt’

In next let we create simple example and will use array splatting.
The first command creates an array of the parameter values and stores it in the $splattingArray variable.The values are in position order in the array.
The second command uses the @splattingArray variable in a command in splatting and you can notice that the $ symbol replaces the dollar sign (@splattingArray) in the command.

$splattingArray = “C:\blog\source.txt”, C:\blog\desination.txt”
Copy-Item -Path @splattingArray

Example 2: Splatting with Hash table

In this example we uses hash table splatting. The first command creates a hash table with key-value pairs and stores it in the $splattingHash variable.
The second command uses the $splattingHash variable in a command with splatting. and you can notice that the $ symbol replaces the dollar sign ($splattingHash) in the command.

 

 

What do you think?

I hope you have an idea of  How to use splatting in Powershell Scriptingl. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.