PoshBytes: PowerShell Splatting – The Art of Throwing Parameters With Style

PoshBytes: PowerShell Splatting – The Art of Throwing Parameters With Style

In this PoshBytes episode, you will learn PowerShell splatting to make long commands readable and reusable. We will also demo PSNotes cmdlets that generate splats for you and convert existing commands into splatted form.

This post is a companion for the video embedded below. Scroll down to see the code from the video.

Splatting basics with a hashtable

Your standard long-form command

Get-ChildItem -Path $env:TEMP -Filter '*.log' -Recurse -File -ErrorAction Stop

A normal command becomes a reusable “parameter bundle” with splatting

$gci = @{
    Path        = $env:TEMP
    Filter      = '*.log'
    Recurse     = $true
    ErrorAction = 'Stop'
}

Get-ChildItem @gci

Same splat, different intent

$gci.Filter = '*.txt'
Get-ChildItem @gci

Second run: real copy, and we add standard parameters again

$copy = @{
    Path        = 'C:\temp\one.txt'
    Destination = 'C:\temp\two.txt'
    Force       = $true
    ErrorAction = 'Stop'
}

Copy-Item @copy -WhatIf

$copy.Destination = 'C:\temp\archive\two.txt'

Copy-Item @copy -Verbose

Conditional splatting without if soup

Conditional paramaeter with splatting and without if spaghetti

param(
    [string]$Path = $env:TEMP,
    [switch]$Recurse,
    [string]$Filter
)

$params = @{
    Path        = $Path
    ErrorAction = 'Stop'
}

if ($Recurse) { $params['Recurse'] = $true }
if ($Filter)  { $params['Filter']  = $Filter }

Get-ChildItem @params

Splatting switches and splatting feels

Switch parameters are just key = $true in your splat

$copy = @{
    Path        = "$env:TEMP\poshbytes.txt"
    Destination = "$env:TEMP\poshbytes.copy.txt"
    WhatIf      = $true
}

Copy-Item @copy

PSNotes time: generate splats and convert commands automatically

Install PSNotes if you do not already have it

Install-Module PSNotes -Scope CurrentUser
Import-Module PSNotes

1) Get-CommandSplatting: print a splat template for any command

Generate a splat template for a command

Get-CommandSplatting -Command 'Get-Item'

Generate a splat template for a command

Get-CommandSplatting -Command 'Get-Item' -Copy

Quick list of parameter sets

Get-CommandSplatting -Command 'Get-Item' -ListParameterSets

Generate splatting for a specific parameter set

Get-CommandSplatting -Command 'Get-Item' -ParameterSet LiteralPath

2) ConvertTo-Splatting: turn an existing command into splatting

Convert a string command into splatting

$splatMe = @'
Copy-Item -Path "test.txt" -Destination "test2.txt" -WhatIf
'@

ConvertTo-Splatting $splatMe

Convert a scriptblock command into splatting

ConvertTo-Splatting -ScriptBlock {
    Get-ChildItem -Path $env:TEMP -Filter '*.log' -Recurse -ErrorAction SilentlyContinue
}

Wrap Up

• Splat named parameters with a hashtable and apply them with @MyParams
• Use conditional splatting to add parameters only when needed
• Get-CommandSplatting generates splat templates and can list parameter sets
• ConvertTo-Splatting converts an existing command string or scriptblock into a splat