Do you ever write a function with so many parameters that it reaches the right end of your screen? In that case, putting all parameters in 1 object might be a usefull replacement, right?

Powershell supports a method to achieve exactly that!

It is called splatting. We create a PowerShell hastable and put all parameters in that object.

The function we use to demonstrate the splatting method when calling a function/method is quite simpel. We only output the passed parameters to the screen. But this is enough for this demo. We use the $PSBoundParameters object to output the parameters directly to the screen.

function Write-Params {
    param (
    # Output parameters to screen with the PSBoundParameters object

Original parameter method

$Path = "C:\temp"
$Computername = "Localhost"
$Message = "Hi, this a message!"

# Run function with seperate parameters, which can be a long line with too many parameters
Write-Params -Path $Path -Computername $Computername -Message $Message

Splatting method

Using the splatting method, we create 1 single “config” object and pass that to the function. This looks a lot cleaner and saves a lot of characters on the line.

# Create a hastable, the key is the parameters name and the value is the parameter's value
$Config = @{
    "Path"         = "C:\temp"
    "Computername" = "Localhost"
    "Message"      = "Hi, this a message!"

# Run function with splatting 
Write-Params @Config


In both cases, the result of running the function will be this:

As Always, choose your method per scenario. Splatting might not be the best way to go for every script/function.

I hope this helps you to write better and readable scripts!

Please check out these articles:
Windows PowerShell: Splatting:
About Splatting:
About Hash Tables: