Powershell: Splatting

Some notes on the method of “splatting” in Powershell code.

Splatting is a method of passing a collection of parameter values to a command as an unit.
Splatting makes your commands shorter and easier to read.

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.

Example 1: Named parameters, via a hashtable:

$HashArguments = @{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }

Copy-Item @HashArguments

Example 2: Positional parameters, via an array:

$ArrayArguments = "test.txt", "test2.txt"

Copy-Item @ArrayArguments -WhatIf