Home Blog Command History Search and Rapid Function Creation
 

Command History Search and Rapid Function Creation

There are two highly innovative features that were introduced in ISESteroids 2.1.0.50. Most of you may not have noticed, so I’d like to give you a little teaser.

Command History Search

Command History Search becomes better with every additional day you use ISESteroids. As you may have noticed, ISESteroids stores your command history, so when you launch ISESteroids the next day, all of your interactive commands are still there  – simply press ArrowUp to browse through your Command History.

Now there is an even smarter way to get back to your commands. Simply start typing something that you know was part of a command in the past. You may want to simply enter the name of a cmdlet you use often.

Now, hold CTRL and press ArrowUp. Each time you press ArrowUp, ISESteroids automatically searches your personal command history, and shows commands that match your current input. The status bar also reports how many matches were found.

This is an extremely powerful technique. Most of the times, we all use the same set of PowerShell commands. With the new Command History Search, you simply enter the beginning of your command, then hold CTRL and press ArrowUp. Chances are ISESteroids can autocomplete what you had in mind, with all parameters and arguments. Press ArrowUp multiple times to see all matches.

Just keep in mind that once you autocompleted your command, this is your new input. So when you release CTRL, and start over again, ISESteroids obviously now takes the current input (the autocompleted one), and you may no longer find what you were looking for.

Note also that ISESteroids persists only commands that ran successfully. Commands that were aborted or produced errors are not part of the persisted command history.

Rapid Function Creation

To even things out, there is also a new feature for the editing part. When writing scripts, a large degree of work is spent on creating PowerShell functions.

If you know what you have in mind, then simply write down the syntax of the function you want to create. Do not worry about squiggles and syntax errors while you do this. Of course, a syntax is no valid PowerShell code. But it will be, in a second.

So for example, write something like this:

Test-MyFunction -Name "Tom" -Age 12 -Force

Next, select your syntax, and right-click. In the context menu, choose “Create Function From Syntax”. ISESteroids takes care of the rest, and the result looks like this:

function Test-MyFunction
{
 [CmdletBinding()]
 param
 (
 [Parameter(Mandatory=$true)]
 [ValidateSet("Tom")]
 [string]
 $Name,
 
 [Parameter(Mandatory=$true)]
 [int]
 $Age,
 
 [Parameter(Mandatory=$false)]
 [switch]
 $Force
 )
 
 
 # TODO: place your function code here
 # this code gets executed when the function is called
 # and all parameters have been processed
 
 
}

Of course, you may want to polish the code. Because you specified a fixed string for -Name, the code generator adds a ValidateSet statement, allowing only that particular text to be submitted. Either add more choices, or remove the ValidateSet attribute. To not get a ValidateSet in the first place, you could have specified a type rather than sample data:

Test-MyFunction -Name <string> -Age 12 -Force

The automatic syntax-to-code conversion is much more powerful. You can even overload your function (use different parameter sets). Try this:

Test-MyFunction -Name "Tom" -Age 12 -Force
Test-MyFunction -Name "Test" -Process <System.Diagnostics.Process>
When you again select your syntax, right-click it, and turn it into a function, the result looks like this:
function Test-MyFunction
{
 [CmdletBinding(DefaultParameterSetName='ParameterSet1')]
 param
 (
 [Parameter(ParameterSetName='ParameterSet1', Mandatory=$true)]
 [Parameter(ParameterSetName='ParameterSet2', Mandatory=$true)]
 [ValidateSet("Tom")]
 [string]
 $Name,
 
 [Parameter(ParameterSetName='ParameterSet1', Mandatory=$true)]
 [int]
 $Age,
 
 [Parameter(ParameterSetName='ParameterSet1', Mandatory=$false)]
 [switch]
 $Force,
 
 [Parameter(ParameterSetName='ParameterSet2', Mandatory=$true)]
 [Diagnostics.Process]
 $Process
 )
 
 
 # TODO: place your function code here
 # this code gets executed when the function is called
 # and all parameters have been processed
 $chosenParameterSet = $PSCmdlet.ParameterSetName
 switch($chosenParameterSet)
 {
 'ParameterSet1' { 'User has chosen ParameterSet1' } 
 }
 
 
 
}

If you want to create even more sophisticated functions, with pipeline support, Impact level, dynamic parameters, etc., then have a look at the navigation bar combo (the bar below the secondary toolbar where you can see all functions in your script).

It has a “star” icon on the left hand side, and when you right-click it, a context menu opens. Choose “From Syntax” (the command is grayed out when your caret is not in a whitespace area so you don’t overwrite existing code).

This opens a function wizard. Either click one of the sample links in the top area to read how the wizard works, or enter your syntax definition. Check “Show Options” to see the advanced options.

It has never been easier to create even the most sophisticated PowerShell functions.