Today’s fun is still PowerShell related, but instead of something in the console, we’ll have some fun with the PowerShell ISE. One of the things I love about the PowerShell ISE is that you can customize it and extend it. My ISE Scripting Geek project is an example. But today I have a simple example that you can try.
When I’m training or creating courses for Pluralsight, I am often in the PowerShell ISE and it would be helpful to highlight the line of code I am going to run. But I don’t want to muck around with a mouse. So I have created an ISE shortcut to select the current line.
The ISE has an object model that has a root of $psise which is automatically created when you launch the ISE. I won’t go into the entire structure now. For our purposes we want to take advantage of this method:
This method takes 3 parameters, the text to display, an action scriptblock and optionally a keyboard shortcut. If you don’t want to use a parameter, use $Null in its place. Sometimes I create menu items that don’t have keyboard shortcuts so I’ll use $Null. But in this case a keyboard shortcut is the primary purpose.
You can use any combination using Ctrl, Alt, or Shift along with function keys (F2) and alphanumeric characters. I’m going to use Ctrl+L because it is unused. If they keyboard shortcut is already used, you’ll get an error when you run the command.
I’ve posted a short script on GitHub to do the job.
Once I run this script, I have a new menu shortcut.
If you make a mistake anywhere or want to re-run the command you’ll need to open a new PowerShell tab or restart the PowerShell ISE and try again.
I expect at some point I’ll incorporate this into the ISE Scripting Geek module, but for now it is separate. For now, I’ll insert the commands into my PowerShell ISE profile script. In the ISE run this command to edit it:
If you’ve never created it you’ll get an error message. The ISE profile script will be something like C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1.
Enjoy your weekend.