PowerShell Splatting: What is it and How Does it Work?

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Splatting is a PowerShell command which lets you execute multiple commands in one line. This article discusses what splatting is and how it works, the problems with using cmdlets all the time, and potential benefits of using this type of command.

PowerShell splatting is a feature that allows the user to write one PowerShell command and have it execute multiple commands. This can be done by using the “splat” operator. Read more in detail here: powershell splatting examples.

PowerShell Splatting: What is it and How Does it Work?

Splatting in PowerShell may seem unusual, but it’s a simple method to format and transmit parameters to cmdlets and functions. You may use splatting to make your code more legible and simpler to use instead of a lengthy list of arguments or those same parameters separated by error-prone backticks.

Learn how to utilize PowerShell splatting to improve your scripts and code in this post!

Prerequisites

If you want to follow along with this post, make sure you have Windows PowerShell 5.1 for the most part, but PowerShell 7.1 to override splatted parameters (demo below).

What is PowerShell Splatting, and how does it work?

To comprehend PowerShell splatting, you must first comprehend the fundamental parameters. When passing parameters to a PowerShell command, you typically use a dash, the parameter name, and then the argument, as seen below.

-WhatIf -Force Copy-Item -Path “TestFile.txt” -Destination “CopiedFile.txt”

Alternatively, you might use backticks to send arguments.

-WhatIf’-Force’Copy-Item’-Path “TestFile.txt”‘-Destination “CopiedFile.txt” ‘

Why would you use the backtick approach versus the usual way? The standard way is excellent, but when there are a lot of factors, it might be difficult to work with and interpret. The use of a backtick, “‘, tends to improve readability significantly. Due of the ease with which a backtick may be forgotten or misplaced, this approach is typically not advised.

Instead, you can use PowerShell splatting. To splat a parameter set, first create a hashtable containing key/value pairs of each parameter and parameter argument. Then, once you have the hashtable built, pass that set of parameters to the command using @<hashtable name>.

For example, you might build a hashtable named $Params and then use the Copy-Item cmdlet to send that hashtable’s set of arguments to the Copy-Item cmdlet, as seen below.

“Path” = “TestFile.txt” “Destination” = “CopiedFile.txt” “WhatIf” = $True $Params = @ “Path” = “TestFile.txt” “WhatIf” = $True $Params = @ “Path” = “TestFile.txt” “WhatIf” = $True $Param $True “Force” = $True “Force” = $True “Force” = $True @Params Copy-Item

Combining Traditional and Splatting Parameters

You may simply mix the two approaches when testing scripts or using the command line. When working with scripts, it’s common to construct a splatted variable to assign to the function or cmdlet. Here’s an example of what this would look like:

@ “Path” = “TestFile.txt” $Params = @ “Path” = “TestFile.txt” “CopiedFile.txt” as “Destination” -Force -WhatIf Copy-Item @Params

As you can see, this is a really helpful strategy that allows you to employ either way depending on your need. By combining methods, you may test an extra parameter or format the parameters differently for formatting.

Splatted Parameters Take Priority

In PowerShell 7.1, you can now override splatted parameters. Prior to this, you could not modify a splatted parameter via a passed parameter. As an example of Splatted Parameters Take Priority, notice how the -WhatIf parameter is overridden below.

“Path” = “TestFile.txt” “Destination” = “CopiedFile.txt” “WhatIf” = $True $Params = @ “Path” = “TestFile.txt” “WhatIf” = $True $Params = @ “Path” = “TestFile.txt” “WhatIf” = $True $Param $True “Force” = $True “Force” = $True “Force” = $True @Params Copy-Item -WhatIf:$False

Changing the value of a splatted parameterChanging the value of a splatted parameter

Splatted Parameters Take Priority allows you to negate the key/value parameter defined in the hashtable and instead, use the value of the parameter defined traditionally.

Positional Parameters Splatting Arrays

Using named parameters is considered best practice since it compels you to give the parameter name followed by the parameter argument. You may, however, splat parameters by location as well.

If you want to transfer a file named TestFile.txt to a file called CopiedFile.txt, for example, you may use positional arguments like the ones below.

‘TestFile.txt’ ‘CopiedFile.txt’ Copy-Item

You may generate an array (rather than a hashtable) using merely the parameter values instead of giving positional arguments the old-fashioned manner. You can see an example of this below.

@ $ParamArray $ParamArray $ParamArray $ParamArray $Par ( “TestFile.txt” “CopiedFile.txt” ) @ParamArray Copy-Item

Using an array to create positional splatted arguments.Using an array to create positional splatted arguments.

Though not often used, this approach may be handy since it provides a less verbose way of giving splatted arguments. Keep in mind that you must be aware of the placements of the arguments in a specific function or cmdlet; otherwise, you risk supplying data to erroneously targeted parameters.

Splatted Commands and Proxy Functions

Sometimes a PowerShell cmdlet isn’t precisely what you’re looking for. You may design a “wrapper” function or even a proxy function in this situation. These functions enable you to add new arguments to an existing cmdlet before calling it with the new parameters.

Getting to Know $Args and $PSBoundParameters

When you launch a PowerShell function, it automatically generates an array variable named $Args. All unnamed argument values given to that function are stored in this array.

Another automated variable named $PSBoundParameters has a hashtable containing all bound parameters. The Test-Function function returns the $Param1 and $Param2 arguments, as seen below.

Function $Param1, $Param2) $Param1 $Param2 $Param1 $Param2 $Param2 $Param1 $Param2 $Param2 $Param $Args Write-Host “Unnamed Parameters” -ForegroundColor ‘Green’ Write-Host “Unnamed Parameters” -ForegroundColor ‘Green’ $PSBoundParameters Write-Host “Bound Parameters” -ForegroundColor ‘Green’ “test1” “test2” “test3” are test-functions. -Param1 “testParam” -Param2 “testParam2” -Param3 “testParam3”

The automated variables $Args and $PSBoundParameters are shown.The automated variables $Args and $PSBoundParameters are shown.

What role do automatic variables play in splatting? Automatically handing in both bound and unbound arguments is highly beneficial when designing a proxy command.

Using Splatted Parameters to Create a Wrapper Function

Create a custom function that provides unnamed and named arguments to the Copy-Item cmdlet to demonstrate how helpful splatting can be in wrapper functions. You may rapidly develop new functions using this method that offer more functionality while keeping the same parameter set as before.

Splatted Parameters Wrapper FunctionSplatted Parameters Wrapper Function

It’s vital to understand that the automatic variable $Args is no longer accessible if you use the CmdletBinding declaration or specify parameter attributes.

Conclusion

Splatting arguments into functions and cmdlets is a great way to improve the readability and usefulness of your code. Because hashtables and arrays are simple to use, you can easily expand on splatted data to conditionally alter values based on choices supplied. Include this effective method in your scripts right now!

PowerShell Splatting is a method of passing parameters to commands in PowerShell. The “powershell splatting array” is an example of the way that this works.

Frequently Asked Questions

What is splatting in PowerShell?

A: Splatting is the art of putting quotation marks everywhere and swishing them all around a sentence. When it was first introduced on the internet, people thought it was an April Fools joke that had gone too far when they saw someone quoting themselves with their own words.

How do you use splatting in PowerShell?

A: Splatting is the act of writing a single character to standard output.

What is a hash table in PowerShell?

A: A hash table is a data structure that can be used to support dictionaries, sets, lists and maps in your PowerShell scripts. The sections below will explain how the different types of hashes work.:

Related Tags

  • powershell splatting parameters
  • powershell splatting function
  • powershell splatting switch parameters
  • powershell function
  • splatting python

Table of Content