🎓 A1Learner
🔍
0 of 47 lessons completed

PowerShell Splatting

Splatting is a way to pass named arguments to a command from a hashtable. The hashtable holds the parameter names and values, and @ in front of its name hands them to the command.

Splat a hashtable to a command

The hashtable holds the date and the format. The @ sign hands both to Get-Date as named arguments.

$params = @{Date = "2026-10-28"; Format = "yyyy-MM-dd"}
Get-Date @params

Splat a switch to a function

The first hashtable turns the switch on. The second hashtable leaves it out, so the switch stays off.

function Show-DemoName {
    param([string]$Name, [switch]$Upper)
    if ($Upper) { $Name.ToUpper() } else { $Name }
}
$upper = @{Name = "ana"; Upper = $true}
Show-DemoName @upper
$plain = @{Name = "ana"}
Show-DemoName @plain

Forward bound parameters to another function

The wrapper takes the same parameters as the inner function. It passes its own bound parameters straight on.

function Get-DemoGreeting {
    param([string]$Name, [switch]$Upper)
    if ($Upper) { $Name.ToUpper() } else { $Name }
}
function Send-DemoGreeting {
    [CmdletBinding()]
    param([string]$Name, [switch]$Upper)
    Get-DemoGreeting @PSBoundParameters
}
Send-DemoGreeting -Name "ana" -Upper
Send-DemoGreeting -Name "ana"

Example

function Format-DemoText {
    param([string]$Text, [switch]$Upper)
    if ($Upper) { $Text.ToUpper() } else { $Text }
}
function Show-DemoText {
    [CmdletBinding()]
    param([string]$Text, [switch]$Upper)
    Format-DemoText @PSBoundParameters
}
$textArgs = @{Text = "hello"; Upper = $true}
Show-DemoText @textArgs

Key points

  • Hashtable: a splat hashtable pairs each parameter name with its value.
  • Use @params instead of $params to hand the pairs to a command as named arguments.
  • Use splatting to keep long commands short and easy to read.
  • Switch: a switch parameter takes $true or $false in a splat hashtable.
  • Use a splat hashtable with no switch entry to leave that switch off.
  • Forwarding: passing a command's own parameters on to another command.

In this example, the hashtable holds the text and the switch, and Show-DemoText receives them by splatting. Show-DemoText then forwards its own bound parameters to Format-DemoText, which turns the text to upper case.

Note

The @ sign in front of a hashtable name is what makes it a splat.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which line passes the hashtable $params to Get-Date as named arguments?