Automatic Parameter Splatting ISE Add-On

Here’s some code I put together which converts parameters in a command to a splatted variant of them. There’s definately room for improvement with the code (it can struggles with some sequences of parameters), so any contributions or ideas would be great for this. It’s posted in my GitHub area.

The function below parses the selected text and attempts to split them into separate chunks of parameters, which are processed and then concatenated into a single string. Once complete, the existing parameters are removed from the command and replaced with the splat, and the hash table placed on the line above.

function ConvertTo-Splat

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [string] $InputObject

    If (!$InputObject) 
        $InputObject = $psISE.CurrentFile.Editor.SelectedText
    $tokens = $null
    $errors = $null
    $ParsedInput = [System.Management.Automation.Language.Parser]::ParseInput($InputObject,[ref]$tokens,[ref]$errors)
    $results = $ParsedInput.FindAll({
            $args[0] -is [System.Management.Automation.Language.CommandElementAst]
    $splatstring = "`r`n`$paramblock = @{`r`n"
    For ($i = 0;$i -lt ($results.count -1);$i = $i+2) 
        $paramName = ($results[$i].Extent) -replace '-', ''
        $firstextent = $results[$i].Extent.Text[0]
        $secondextent = $results[$i+1].Extent.Text[0]
        If($firstextent -eq $secondextent) 
            $value = '$true'
            $i = $i - 1
            $value = $results[$i+1].Extent
            If ($value -notlike '*$*') 
                $value = "`"$value`""
        $splatstring += "$paramName = $value`r`n"
    $splatstring += "}`r`n`r`n"
    $currentLine = $psISE.CurrentFile.Editor.CaretLine

Run the function to store it memory. Now, in order for us to be able to call the function either from a menu or keyboard option in the ISE, run the following code. :

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Get Splat', { 
        $text = $psISE.CurrentFile.Editor.SelectedText
        ConvertTo-Splat -InputObject $text
}, 'ALT+T')

With this done, all that’s required is to :

  • Select the parameters you wish splatted in the command
  • ¬†Either
    • Click Add-ons in the menu bar
    • Click Get Splat
  • ¬†Or
    • Press ALT + T
Select the parameters

Selection of the parameters and splatting option

  • View the results
The result of the autosplat

The results of the autosplat

Here’s a video of it in action:


Leave a Reply

Your email address will not be published. Required fields are marked *