IT Questions and Answers :)

Wednesday, August 28, 2019

In PowerShell, what is "splatting"?

In PowerShell, what is "splatting"?

  • A way to pass multiple parameters to a cmdlet
  • A way to define many variables at once
  • An error state where many things failed at once
  • A way to dump the full contents of the pipeline 


Splatting to pass parameters to commands in PowerShell

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 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.
Beginning in Windows PowerShell 3.0, you can also use splatting to represent all parameters of a command.


<CommandName> <optional parameters> @<HashTable> <optional parameters>
<CommandName> <optional parameters> @<Array> <optional parameters>
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.
When splatting, you do not need to use a hash table or an array to pass all parameters. You may pass some parameters by using splatting and pass others by position or by parameter name. Also, you can splat multiple objects in a single command just so you pass no more than one value for each parameter.




Post a Comment

Popular Posts