TExcelTAD
Home › PowerShell › PowerShell Splatting

PowerShell Splatting

Some commands need many parameters, and long command lines are hard to read and easy to break. Splatting solves this: you put the parameters in a hashtable (or array) and pass it to the command with @ instead of $.

Hashtable splatting

powershell · try it yourself
# Without splatting: one very long line
New-Item -Path ./reports -Name 'summary.txt' -ItemType File -Value 'Q1 results' -Force | Out-Null

# With splatting: one parameter per line
$fileParams = @{
    Path     = './reports'
    Name     = 'notes.txt'
    ItemType = 'File'
    Value    = 'Meeting notes'
    Force    = $true
}
New-Item @fileParams | Out-Null

Get-ChildItem ./reports | ForEach-Object Name
Get-Content ./reports/notes.txt
notes.txt
summary.txt
Meeting notes

The keys are the parameter names without the dash. A switch parameter like -Force becomes Force = $true. Notice the @fileParams: with $fileParams the whole hashtable would be passed as one positional argument instead.

Array splatting

An array is splatted as positional arguments, in order:

powershell · try it yourself
function Get-Volume {
    param($Length, $Width, $Height)
    "$Length x $Width x $Height = $($Length * $Width * $Height)"
}

$box = 2, 3, 4
Get-Volume @box
2 x 3 x 4 = 24

Hashtables are usually clearer, because the names document what each value means.

Mixing splatting with normal parameters

You can combine a splatted hashtable with extra parameters, and even splat several hashtables. A common pattern is a set of shared defaults plus per-call differences:

powershell · try it yourself
function Send-Report {
    param($To, $Subject, $Server = 'localhost', $Port = 25, [switch]$UseSsl)
    "To=$To | Subject=$Subject | $Server`:$Port | SSL=$UseSsl"
}

$mailDefaults = @{
    Server = 'smtp.example.com'
    Port   = 587
    UseSsl = $true
}

Send-Report @mailDefaults -To 'ops@example.com' -Subject 'Disk report'
Send-Report @mailDefaults -To 'dev@example.com' -Subject 'Build failed' -Port 2525
To=ops@example.com | Subject=Disk report | smtp.example.com:587 | SSL=True
To=dev@example.com | Subject=Build failed | smtp.example.com:2525 | SSL=True

In PowerShell 7 an explicit parameter overrides the same key from the splatted hashtable, as -Port 2525 shows. (Windows PowerShell 5.1 reports an error for the duplicate instead.)

Building parameters conditionally

Because the hashtable is just data, you can add parameters only when they're needed. This avoids duplicated command lines inside if statements:

powershell · try it yourself
function Find-Files {
    param([string]$Folder, [string]$Filter, [switch]$Deep)
    $gciParams = @{ Path = $Folder; File = $true }
    if ($Filter) { $gciParams.Filter = $Filter }
    if ($Deep)   { $gciParams.Recurse = $true }
    Get-ChildItem @gciParams | ForEach-Object Name | Sort-Object
}

New-Item -ItemType Directory ./proj/src -Force | Out-Null
'a.ps1', 'b.txt', 'src/c.ps1' | ForEach-Object { Set-Content "./proj/$_" 'x' }

Find-Files -Folder ./proj -Filter *.ps1
'---'
Find-Files -Folder ./proj -Filter *.ps1 -Deep
a.ps1
---
a.ps1
c.ps1

Passing parameters through: $PSBoundParameters

Inside a function, $PSBoundParameters is a hashtable of the parameters the caller actually passed. Splat it to forward them to another command, for example in a wrapper function:

powershell · try it yourself
function Get-Square {
    param([int]$Number, [switch]$Negative)
    $n = $Number * $Number
    $Negative ? -$n : $n
}

function Get-SquareLogged {
    param([int]$Number, [switch]$Negative)
    "Called with: $(($PSBoundParameters.Keys | Sort-Object) -join ', ')"
    Get-Square @PSBoundParameters
}

Get-SquareLogged -Number 6
Get-SquareLogged -Number 6 -Negative
Called with: Number
36
Called with: Negative, Number
-36

Tip: Splatting also makes scripts easier to review in version control: a change to one parameter is a change to one line.

Test yourself

How do you splat the hashtable $opts into Copy-Item?

  1. Copy-Item $opts
  2. Copy-Item @opts
  3. Copy-Item -@opts
  4. Copy-Item %opts

How is a switch parameter like -Recurse written in a splatting hashtable?

  1. Recurse = 'on'
  2. Recurse = $true
  3. '-Recurse'
  4. It can't be splatted

What does $PSBoundParameters contain?

  1. All parameters with their defaults
  2. Only the parameters the caller actually passed
  3. The script's arguments as strings
  4. Global variables