What is splatting in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

PowerShell splatting is a method to pass the collection of the parameters as a single command unit which makes the command shorter and easier for the user to read commands. Splatting uses the symbol (@) instead of ($) which tells the user that splatting is used and PowerShell is passing a set of values instead of a single value.

Splatting in PowerShell was included from the v3.0 onwards and you can pass all parameters in the command.

For example,

$params = @{
   Path = 'C:\Temp\25Aug2020.txt'
   Destination = 'C:\test1'
   Verbose = $true
   Force = $true
}
Copy-Item @params

The splatting parameters can be used with Hashtable and with an Array.

Hashtable splatting is a combination of the Name and value pair. You can use this format for all parameter types, including Positional and Switch parameters. Positional parameters must be assigned by name.

Array Splatting uses values of the positional parameter, which do not require parameter names. The values must be in the positional number order in an array.

Positional parameters can be identified with the Help command. For example, to identify the Copy-Item positional parameters, use the below command.

help Copy-Item -Parameter *

raja
Published on 18-Jan-2021 07:19:25
Advertisements