How To Use Splatting To Pass Parameters To Commands


Many a times we might have come across the situation where we need to execute the lengthy CmdLets/functions having bunch of parameters and exceeds screen width and wrapped down to the next line or need to scroll towards end of the command or need to ignore the new line using the escape character ( ).

Splatting, pass the parameter values as a collection in the form of name and value pair, as a hash table or array of values. It makes the command shorter, easy to read and can be re-used. It’s a hash table/array variable though, to pass the parameter values to the command @ symbol will be used before the variable name instead of $.

SYNTAX

$paramtable = @{
Name1 = 'Value1'
Name2 = 'Value2'
Name3 = 'Value3'
}
C:\> Sample-Command @paramtable
or
C:\> Sample-Command <optional parameters> @paramtable <optional parameters>

To provide the named parameter values hash table can be used and to provide the positional parameters array can be used. When splatting, it is not necessary to use either hash table or an array only to pass the parameters, positional parameters and/or named parameters can also be used along with.

EXAMPLE: Splatting with hash table

Create a new file using New-Item CmdLet by passing necessary parameters…

# Along with the named parameters
New-Item -Path C:\Windows\Temp\ -Name Delete.txt -ItemType File -Value "Hello World!" -Force

# With hash table
$paramtable = @{
      Path      = 'C:\Windows\Temp\'
      Name      = 'Delete.txt'
      ItemType  = 'File'
      Value     = 'Hello World!'
      Force     = $true
}

New-Item @paramtable

EXAMPLE: Splatting with array

Copy a file from one location to other using Copy-Item CmdLet by passing necessary parameters…


# Copy a file using named parameters
Copy-Item -Path $env:windir\Temp\CSV1.csv -Destination $env:TEMP\CSV1.csv -Force

# With array
$paramarray = @("$env:windir\Temp\CSV1.csv", "$env:TEMP\CSV1.csv")
Copy-Item @paramarray -Force

An another example…


Function Create-NewItem
{
    [CmdLetBinding(SupportsShouldProcess)]
    param
    (
        [parameter(mandatory=$true,parametersetname="Path")]
        [parameter(mandatory=$false,parametersetname="Name")]
        [string]$Path,
        [parameter(mandatory=$true,parametersetname="Name")]
        [string] $Name,
        [parameter(mandatory=$false,parametersetname="Path")]
        [parameter(mandatory=$false,parametersetname="Name")]
        [string]$ItemType,
        [parameter(mandatory=$false,parametersetname="Path")]
        [parameter(mandatory=$false,parametersetname="Name")]
        [object] $Value,
        [parameter(mandatory=$false,parametersetname="Path")]
        [parameter(mandatory=$false,parametersetname="Name")]
        [switch]$Force,
        [parameter(mandatory=$false,parametersetname="Path")]
        [parameter(mandatory=$false,parametersetname="Name")]
        [pscredential] $Credential
    )
    Write-Host "Creating a new $($ItemType.ToLower())"
    New-Item @PSBoundParameters | Out-Null
    if ($?) {Write-Host "New $($ItemType.ToLower()) has been created successfully"}
}

$paramtable = @{
    Path = "C:\Temp\"
    ItemType = "Directory"
}

Create-NewItem @paramtable

<#
PS C:\> Create-NewItem @paramtable
Creating a new directory
New directory has been created successfully
#>

#Run the script again
Create-NewItem @paramtable

<#
PS C:\> Create-NewItem @paramtable
Creating a new directory
New-Item : An item with the specified name C:\Temp already exists.
At line:25 char:5
+     New-Item @PSBoundParameters | Out-Null
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceExists: (C:\Temp:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
#>

$paramtable.Force = $true

<#
PS C:\> $paramtable
Name                           Value
----                           -----
Path                           C:\Temp\
Force                          True
ItemType                       Directory
#>

#Run the script again
Create-NewItem @paramtable

<#
PS C:\> Create-NewItem @paramtable
Creating a new directory
New directory has been created successfully
#>

$paramtable.Name = 'Test.txt'
$paramtable.ItemType = 'File'
$paramtable.Remove("Force")

<#
PS C:\> $paramtable
Name                           Value
----                           -----
Path                           C:\Temp\
Name                           Test.txt
ItemType                       File
#>

#Run the script again
Create-NewItem @paramtable

<#
PS C:\> Create-NewItem @paramtable
Creating a new file
New file has been created successfully
#>

Share it on     |   |   |   | 
  Prev:  

Prompt for choice

  :Next  

Host A Static Website In A Azure Storage Account

comments powered by Disqus