Friday Fun: Well That’s a Fine ToDo!

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?

Continue reading

Jumping in the PowerShell ISE

During my day I may be working on multiple files in the PowerShell ISE. Often these files are part of different modules and since I use git I generally need to be in the same directory as the file I’m working on. This necessitates a lot of manually changing location in the PowerShell ISE prompt.  Or I might open the PowerShell ISE and then select some files from MRU list. But again, if I need to jump to the corresponding directory, that can be a tedious process. Continue reading

Formal Remote ISE Connections

I have updated my PowerShell tools that make it easier to open up a remote tab in the PowerShell ISE.  The standard approach is limited and doesn’t allow for things like using SSL or different ports.  I have had the ISERemoteTab project up on GitHub for awhile.

I recently added a WPF-based form to make it easier to make remote connections.

remoteIsetabYou can enter in a single computer or multiple computers.  Although, if you use multiple computers, the same settings will apply to all of them. But once I hit connect, I get new remote tabs.

remotetabsIn this example I am using the remote profile script feature to run a set of PowerShell commands after making the remote connection.

In my ISE profile script I added these lines to create a new Add-On shortcut.

All of this has also been published to the PowerShell Gallery. If you are running PowerShell 5 you can use Install-Module to install  ISERemoteTab.

I hope you’ll try it out and let me know what you think. If you run into bugs or have feature requests, please post them on the Github project site.

Friday Fun: Tweaking the PowerShell ISE

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:

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add()

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.

image

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.

Regions Rule the PowerShell ISE

Whenever I teach PowerShell and we get to talking about the PowerShell ISE, I encourage people to get in the habits of using regions. A region can help you organize your code and hide it when you don’t need the distraction. I think regions are especially helpful if you are creating a module file with multiple function definitions. For those of you who don’t know what a region looks like, it is a set of comments which are case sensitive and need to be all lower case.

Whatever code you put in between will be collapsed.

An expanded regionAn expanded region (Image Credit: Jeff Hicks)/em>

A collapsed region
A collapsed region (Image Credit: Jeff Hicks)

But, you can make your regions descriptive by adding text after the key word region.

Adding a region descriptionAdding a region description (Image Credit: Jeff Hicks)

When you collapse the region, you can still see the description. Because I like to use regions, I put together a simple function you can use in the PowerShell ISE to automate the process.

I also defined an alias to make it even easier. Once loaded in the ISE, assuming my cursor is in position for where I want to add the region I can type this command:

Creating a new region
Creating a new region (Image Credit: Jeff Hicks)

And get this result:

The new region
The new region (Image Credit: Jeff Hicks)

Eventually I will roll this into my ISE ScriptingGeek project. But for now, you can add it to the ISE Add-Ons menu with code like this:

The new add-on
The new add-on (Image Credit: Jeff Hicks)

I added an extra bit of code so that you can get a GUI prompt so that when you click to insert a region:

The description prompt
The description prompt (Image Credit: Jeff Hicks)

The new region
The new region (Image Credit: Jeff Hicks)

If you click Cancel or don’t enter anything, you’ll still get a region but without any description.

You could use this tool at the start of a new project by going through and outlining what you need to accomplish with regions. When finished, your code is organized and even partially documented.

Enjoy!