Skip to content

Instantly share code, notes, and snippets.

@craig-martin
Created June 1, 2016 03:03
Show Gist options
  • Save craig-martin/8054b7d232319dc2bff3dc71b841ea06 to your computer and use it in GitHub Desktop.
Save craig-martin/8054b7d232319dc2bff3dc71b841ea06 to your computer and use it in GitHub Desktop.
###
### Without splatting
###
Get-Process -Name dns -ComputerName localhost
###
### With splatting
###
$myparamas = @{
Name = 'dns'
ComputerName = 'localhost'
}
Get-Process @myparamas
###
### Using Splatting with $PSBoundParameters
###
function Get-Foo
{
Param
(
# NOTE: Name maps to the parameter -Name of Get-Process
$Name,
# NOTE: ComputerName maps to the parameter -ComputerName of Get-Process
$ComputerName,
# NOTE: Param1 does NOT map to any parameters of Get-Process
$Param1
)
### Clean up the parameter that does NOT map
if ($PSBoundParameters.ContainsKey('Param1'))
{
$PSBoundParameters.Remove('Param1')
}
### Splat away!
Get-Process @PSBoundParameters
}
### Demo
Get-Foo -Name dns -ComputerName localhost -Param1 baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment