PowerShell: CTRL+E, instead of F8 for SQL folks to run selection in ISE

Hotkey to run selection in PowerShell ISE is F8.

Hotkey to run selection in SSMS is CTRL+E.

For those of us constantly switching back and forth between the two, you know very well how many times we hit the “other” hotkey!

Well, now you can have the same key to run selection in both SSMS and ISE.

Jeff Hicks has written an article on creating new Hot Keys.

All I had to do to make CTRL+E run selection in ISE was “Google” it. I found this StackOverflow answer.

Here is the simple procedure:

  1. Open ISE
  2. Run”Notepad $Profile”
  3. Add below code to the end, save the file and restart ISE
#
#Allow CTRL+E to run selection like F8 does
#
function invoke-selection
{
    iex $psISe.CurrentFile.Editor.SelectedText
}
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
    "Run selection",
    {invoke-selection},
    'ctrl+e')

You will also see this hotkey added to the bottom of the “Add-ons” menu in ISE. Now, you are all set to use CTRL+E to run selection within the ISE! You can even reassign other hotkeys that have been your pet peeves!

Update – Mar 22, 2016:

After using this for a bit, I found that this only works if the selection is all-encompassed and completely self-contained (without any dependencies on variables outside the selection). It is still great but I wish there was a way to get around the limitation. Also, instead of displaying the commands, it displays “invoke-selection” which can be distracting to some.

Advertisements

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 )

Google+ photo

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

Connecting to %s