liveBook · Manning

Parameter splatting

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Splatting is a PowerShell technique that supplies a collection of parameter names and values to a command as invocation arguments.

Parameter splatting supplies a command with a collection of parameter names and values. Build a hash table, use parameter names as keys, and pass the table with @ instead of $. section 12.1

Use splatting to make day-to-day automation easier to maintain:

  • Put each parameter on its own line so command configuration is easier to read and stays within a practical code width. section 12.1
  • Represent switch parameters with $True, just as with the Verbose parameter. section 12.1
  • Reuse and modify parameter tables when several calls share options or when a query changes by selected data type. section 18.14
  • Prefer splatting over repeated command text when repetition makes scripts cumbersome; cleaner code is easier to read and debug. section 18.14

A CIM query with named parameters

Store ClassName and ComputerName in $params, then invoke Get-CimInstance @params. Add Verbose=$True when the command needs that switch. The same pattern supports session-based queries by storing CimSession, filters, or other values in separate parameter tables. section 12.1

A reusable query pattern

Start with a ComputerName entry, add a ClassName entry inside a Switch branch for OS, CS, CPU, or Memory, then splat the completed table into Get-CimInstance. Build the property list alongside it and pass that list to Select-Object. section 18.14

$params = @{'ClassName'='Win32_OperatingSystem'
            'ComputerName'='CLIENT1'}

Inspect the compact progression from constructing a parameter table to invoking a command with splatting. section 12.1

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains construction, switch values, invocation, reuse, and dynamic modification, but does not detail hash-table internals.
Examples
Demonstrates Get-CimInstance queries, Verbose switches, session-based system information, and a service-logon function.
What it assumes
Assumes familiarity with PowerShell hash tables and arrays while teaching the splatting syntax and its automation payoff.
What you can do after
Apply it via PowerShell hash tables to write clearer, reusable commands and functions with changing parameter sets.
How it connects
Connects hash tables, arrays, PowerShell functions, Get-CimInstance, CIM sessions, switch statements, and pipeline processing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.