Splatting parameters in the Powershell ISE, revisited

 Revisiting the idea of using splatting for commands that produce long lines of paramaters and arguments I decided to try leveraging the built in Command Add-On to simplify the command generation.  This add-on provides a convenient means of creating commands using a simple “fill-in-boxes”  format for arguments, and checkboxes for switches. 


but the output it produces is a single command line with all the parameters and arguments inline.   The following script is my attempt to simplify the process of converting that to a more script-friendly format using parameters and arguments splatted from a hash table.

function SplatFromBuffer{

$cmd = [windows.forms.clipboard]::GetText()

$errors = @()
$tokens = [system.management.automation.psparser]::Tokenize($cmd, [ref]$errors)

try {get-command $tokens[0].content -ea Stop}
   catch {
           throw 'Parse failed'

$Pad = $tokens |
  Where {$_.Type -eq 'CommandParameter'} |
   select -ExpandProperty Content |
    sort length |
     select -Last 1 |
      select -ExpandProperty length

$ParamHash = '${0}_Params' -f ($tokens[0]).content.replace('-','')

$Indent = ' ' * ($ParamHash.Length + 7)

$Scriptblock = @"
   $ParamHash = @{


$i = 1
$line = $null

while ($i -lt $tokens.count) {

  if (
       $tokens[$i].Type -eq 'CommandParameter'

         if ($line) {$Scriptblock += ($line + "`n")}

         $line = $indent + $($tokens[$i].content).trimstart('-') + ' ' * ($pad - $tokens[$i].content.length) + '  =  '

         if (
             ($tokens[$i+1] -eq $null) -or
             ($tokens[$i+1].Type -eq 'CommandParameter')
               $line += '$True'


    if (
         $tokens[$i].Type -ne 'CommandParameter'
           Switch ($tokens[$i].Type) {

             'Variable'          {$line += '$' + $tokens[$i].Content}

             'Operator'          {$line += $tokens[$i].content}

             $null               {}

             Default             {$line += '"' + $tokens[$i].Content + '"'}


 $Scriptblock += ($line + "`n")
 $Scriptblock += ($Indent + '}' + "`n`n")
 $Scriptblock += '    {0} @{1}' -f $tokens[0].content,$ParamHash.TrimStart('$')
 $Scriptblock += ("`n" + '  }')
 $ise_current_file = $psise.CurrentFile

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Splat From Buffer",{SplatFromBuffer},"Ctrl+Shift+S")


Adding that code to your ISE profile enables an Add-On (splatfrombuffer) and assigns it to the hot-key combination Ctrl+Shift+S.  With this, you can generate your command line from the Command Addd-On, or by using Show-Command. When you’ve finished specifying your switches and arguments, us the Copy command at the bottom of the pane to copy the generated command line to the clipboard.  From the Add Ons menu, or using the assigned hotkeys invoke the splatfrombuffer add on, and it will read that command line from the clipboard and generate and paste a scriptblock into the script pane to exeucte that command using a parameter hash.


The command is created as a standalone script block to prevent any conflict with existing variables in the script, but you can easily remove that if you don’t need it. 

Comments and suggestions welcome…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s