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:


Creating a Shortcut for a Button in a PowerShell GUI

While most of us are all still friends with the mouse in GUIs, keyboard shortcuts once learned are very effective ways of manipulating form controls quicker.

Serveral controls, such as buttons can use keyboard accelerators. This is simply done by prefixing the Text property of the control by an ampersand. By doing this, you can perform the same as the Click event for that control simply by holding down the ALT key on the keyboard, and then the appropriate letter.

For example, a button control with its text property set to &OK will respond to the keyboard combination ALT + O as if the mouse had clicked on it. In keeping with normal Windows functionality, you can identify the keyboard shortcut by holding down the ALT key, and the letter for the accelerator will be listed with an underline.

Here’s how you’d do this in PowerShell Studio :

Keyboard Shortcut

And in PowerShell code itself, this is represented as :

$buttonOK.Text = '&OK'