Splatting

Splatting is a method of passing multiple parameters to a command as a single unit. This is done by storing the parameters and their values as key-value pairs in a hashtable and splatting it to a cmdlet using the splatting operator @.

Splatting can make a command more readable and allows you to reuse parameters in multiple command calls.

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.


Syntax

To provide parameter values for positional parameters, in which parameter names are not required, use the array syntax. To provide parameter name and value pairs, use the hash table syntax. The splatted value can appear anywhere in the parameter list.

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

Splatting with hash tables

Declaring the splat is useful for reusing sets of parameters multiple times or with slight variations:

$splat = @{ Class = "Win32_SystemEnclosure" Property = "Manufacturer" ErrorAction = "Stop" } Get-WmiObject -ComputerName $env:COMPUTERNAME @splat Get-WmiObject -ComputerName "Computer2" @splat Get-WmiObject -ComputerName "Computer3" @splat

However, if the splat is not indented for reuse, you may not wish to declare it. It can be piped instead:

@{ ComputerName = $env:COMPUTERNAME Class = "Win32_SystemEnclosure" Property = "Manufacturer" ErrorAction = "Stop" } | % { Get-WmiObject @_ }

Splatting with arrays

Use an array to splat values for positional parameters, which do not require parameter names. The values must be in position-number order in the array.

Copy-Item "test.txt" "test2.txt" -WhatIf $ArrayArguments = "test.txt", "test2.txt" Copy-Item @ArrayArguments -WhatIf

Using the ArgumentList parameter

Several cmdlets have an ArgumentList parameter that is used to pass parameter values to a script block that is executed by the cmdlet. The ArgumentList parameter takes an array of values that is passed to the script block.

$array = 'Hello', 'World!' Invoke-Command -ScriptBlock { param([string[]]$words) $words -join ' ' } -ArgumentList $array

In this example, $array is wrapped in an array so that the entire array is passed to the script block as a single object.

$array = 'Hello', 'World!' Invoke-Command -ScriptBlock { param([string[]]$words) $words -join ' ' } -ArgumentList (,$array)

Passing a Switch parameter using Splatting

To use Splatting to call Get-Process with the -FileVersionInfo switch similar to this:

Get-Process -FileVersionInfo #This is the call using splatting: $MyParameters = @{ FileVersionInfo = $true } Get-Process @MyParameters $MyParameters = @{ FileVersionInfo = $true } Get-Process @MyParameters -Name WmiPrvSE Get-Process @MyParameters -Name explorer

Splatting From Top Level Function to a Series of Inner Function

Without splatting it is very cumbersome to try and pass values down through the call stack. But if you combine splatting with the power of the @PSBoundParameters then you can pass the top level parameter collection down through the layers.

Function Outer-Method { Param( [string] $First, [string] $Second ) Write-Host ($First) -NoNewline Inner-Method @PSBoundParameters                } Function Inner-Method { Param( [string] $Second ) Write-Host (" {0}!" -f $Second) } $parameters = @{ First = "Hello" Second = "World" } Outer-Method @parameters