Today’s Friday Fun isn’t exactly groundbreaking but you might find it useful in your PowerShell script development process. You might even learn a little something about the PowerShell ISE which is really the point of these articles anyway. How many times have you been working on a script or PowerShell tool and know that you’ll have to write some section of code but aren’t ready to tackle it now? Hopefully you are at least savvy enough to insert a comment reminding you what you need to do. So why not make this simple step as easy as possible in the PowerShell ISE?
Recently I published a PowerShell function that I use to display a random Git Tip of the Day. The function relies on my clone of the Git-Tips project on GitHub. I’ve been keeping tabs on this project and a question was posed about creating a command line utility to search the tip list. Now, this project is most likely aimed at developers, at least based on what I’ve seen in the list of questions and issues. But for PowerShell people, this is a pretty simple thing to pull together.
This year I’ve really taken to learning Git and how to incorporate it into my daily work routine. If nothing else this has been a great reminder about what it is like to learn something totally new and foreign. I’ve learned quite a bit, but am far from considering myself a master. Git is a big topic so I’m always looking for new ways to learn and use it. Continue reading
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.