Using Splatting

by Apr 17, 2015

With splatting, you can call cmdlets and programmatically control the parameters you submit.

To do this, add the parameters and values to a hash table, then submit the hash table to the cmdlet. This works with any cmdlet.

Here is an example:

# classic:
Get-ChildItem -Path c:\windows -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue

# Splatting
$params = @{}
$params.Path = 'c:\windows'
$params.Filter = '*.ps1'
$params.Recurse = $true
$params.ErrorAction = 'SilentlyContinue'
Get-ChildItem @params

Twitter This Tip! ReTweet this Tip!