Using the PowerShell ISE Editor Object

Posted on October 6, 2014 by Jeff Hicks in PowerShell with 0 Comments

In my last article, I introduced you to the PowerShell ISE object model. In the Windows PowerShell ISE, you can view this built-in object by using the following command:

Where this really gets fun is with the editor. With the editor, you can modify script contents directly from PowerShell. You’ll see why this is useful.

The PowerShell ISE Editor Object

Each ISE file object has an editor property, which is in itself an object.

The PowerShell ISE Editor Object. (Image Credit: Jeffery Hicks)

The PowerShell ISE Editor Object. (Image Credit: Jeffery Hicks)

The editor object wouldn’t be much use if you couldn’t do things with it. Pipe the object to Get-Member to discover what it can do.

Using Get-Member with the PowerShell ISE Object Editor. (Image Credit: Jeffery Hicks)

Using Get-Member with the PowerShell ISE Object Editor. (Image Credit: Jeffery Hicks)

Inserting Text

Adding text to a file is pretty easy. The following command will insert the comment in the current file at the current cursor location:

You can also move the cursor around. To make things easier, let’s save the editor object to a variable.

Next, I’m going to move to the last line of the script.

The SetCaretPosition() method requires parameter values for the line and column numbers, starting at 1. Now I can insert more text.

Notice I’m using single quotes for the value. This is so the $Now is not treated as a variable but rather a literal string so that my script now has the line $Now = Get-Date.


Sending output to a tab

You’re probably thinking this is a lot of work when all you have to do is type. And you would be right. But where this gets interesting is when you start building tools around these methods and objects. I have been manually demonstrating some techniques. Creating a tool is where we want to go. For example, I often run a command and want to view the results in a temporary text file. If I’m in the PowerShell ISE, then I can create a new file and insert text using the PSISE model.

Here’s my code.

This function is designed to take pipelined input. The working code is in the End scriptblock when I create a new file and then insert data as text.

The default is an untitled PS1 file. But I included a Path parameter so you can save the output to a file at the same time.

Now I’m working smarter.


Modifying Scripts

It can be a bit tricky inserting text, especially around existing text, so here is a demonstration script, that you could actually use in the real world. It inserts a comment at the beginning of the script and appends a line at the bottom. The `n characters tell PowerShell to insert a new line.

With this script I can update any number of files from the ISE.

This is a simple example.

The more you experiment with the ISE object model, the more you’ll realize what you can do with it. In the next article, I’ll show you how to incorporate the ISE tools you are developing into the ISE itself.


Tagged with ,