Tag Archives: ISE

What Were You Working On?

toolboxIt probably comes as no surprise that I write a lot of PowerShell code. Like you, I’m usually working on several projects at the same time, most often using the PowerShell ISE. When I fire up the PowerShell ISE I often go to most recently edited files and re-open the files I was last working on. But sometimes my list of current projects gets pushed aside by other files I might open and edit. Because I like to be lazy (I mean efficient) I decided to come up with a solution to make it easy to open files I’m actively developing.

In the PowerShell ISE you can easily load a file into the editor with the PSEDIT command.

So all I need is a way to store my active files. It is simple enough to use a text file. All the text file needs is a list of full file paths. Opening all the files can be as easy as this:

But how can I easily add a file to the list? I can use the PSISE object model to get the path for the currently active script file and add it to the file.

To remove a file from the list, it is probably easiest to simply open the current work list using PSEDIT and manually delete what I no longer need. I took these core commands and wrapped them in a set of functions. And of course, I don’t want to have to type any more than I have to so it would be handy to add some shortcuts to the ISE Add-On menu.

I ended up creating a set of functions for these key operations and incorporated them into my ISE Scripting Geek module. The module exports a global variable for my work list.

Now when I open the ISE I can press Ctrl+Alt+I and have immediate access to everything I’m currently working on. This has already saved me a lot of time and frustration.

I have moved the ISE Scripting Geek module to GitHub so check it out here. Feel free to pick and choose what you want from the module or take my commands here and create your own solution for loading your active work files. The functions for this particular feature are in CurrentProjects.ps1.

Enjoy and let me know if this starts saving you some time.

Friday Fun: A Better PSEdit

In the PowerShell ISE, there is a built-in function called PSEdit. You can use this function to easily load a file in to the ISE directly from the ISE command prompt.

You can also load multiple files, but not as easily as you might like. I find myself wanting to do this:

As you can see isn’t what I’m expecting. I can get PSEdit to open multiple files, but I need to use a command like this:

I finally tired of this so I looked at the code for the PSEdit function.

I am assuming based on what I see that this was written a long time ago. So I decided to update it. Here’s my version:

The major difference is that my version works in the pipeline making it easier, for me at least, to open multiple files at once.

I also added some verbose messages for troubleshooting. This is my common practice when creating new PowerShell tools. You’ll also notice that I replaced the aliases in the original function with complete cmdlet and parameter names.

The last “feature” is my customized ValidateScript attribute. I wanted to verify that any path pointed to a legitimate file. I could have simply used this:

But if the path failed the test, PowerShell displays a long error message that isn’t always helpful. So I added some logic. Validation tests have to return either True or False. When it is false, PowerShell throws the exception. So I wrote my own exception message.

I get a similar error with the original psedit.

So perhaps I haven’t improved on it that much. But I could have written an even longer message and I wanted to demonstrate this technique in case you wanted to use it.

One last word on my version of PSEdit. I didn’t use a standard name, I guess because the original function doesn’t use one. And I’m ok with that. This is one of the situations where the function is a “cheater” command with a simple, alias-like, name. If you want to replace the original PSEdit function, add mine to your ISE profile script and rename it to PSEdit.


Send from PowerShell ISE to Microsoft Word Revisited

Many of you seemed to like my little PowerShell ISE add-on to send text from the script pane to a Word document. I should have known someone would ask about a way to make it colorized. You can manually select lines in a script and when you paste them into Word they automatically inherit the colorized tokens. Unfortunately, coming up with a PowerShell equivalent is much more complicated.

If you search around you’ll find plenty of tools and scripts for generating HTML and colorized output from the ISE. I tried incorporating some of them into my script but they were much more complicated than I wanted to deal with. All I really needed was a simple Ctrl+C command. So I cheated. I decided to use the SendKeys() method from VBScript.

I added a new switch parameter to the function called Colorized. This meant I also needed an additional menu shortcut.

You’ll notice that there is no keyboard shortcut. At least for me, I got inconsistent results using a keyboard shortcut, and often nothing. But if I selected the menu item, it always seemed to work.

Here is the complete updated function.

I can’t guarantee the color copy and paste will work 100% of the time. Otherwise, you can always use traditional keyboard shortcuts: Ctrl+C,Alt+Tab (to Word), Ctrl+V.