An Overview of the PowerShell ISE Object Model

Last Update: Sep 04, 2024 | Published: Sep 26, 2014

SHARE ARTICLE

Over the last several articles I’ve demonstrated a number of techniques for working efficiently in the PowerShell ISE. In addition to the built-in shortcuts I’ve shared with you, there is also an option to create your own shortcuts. The PowerShell ISE has its own object model. While it isn’t that complicated it will take a few articles to fully explain everything I want to share with you, so let’s get started.

The PSISE object

Open the ISE, and at the prompt look at $psise, which is a built-in object.

Using the $psise command in the Windows PowerShell ISE.
Using the $psise command in the Windows PowerShell ISE. (Image: Jeff Hicks)

As with any new object in PowerShell, you should pipe it to Get-Member.
Using $psise | get-member in the Windows PowerShell ISE.
Using $psise | get-member in the Windows PowerShell ISE. (Image: Jeff Hicks)

The properties themselves are nested objects. For example, the CurrentPowerShellTab property is an ISE Tab object.
Using $psise.CurrentPowerShellTab in the Windows PowerShell ISE.
Using $psise.CurrentPowerShellTab in the Windows PowerShell ISE. (Image: Jeff Hicks)

Some of these properties you can set. The PowerShellTabs property is a collection of these types of objects. I’ll show you how to take advantage of this property in a future article.

Accessing the current file

What you will find especially useful is that you can access information about the current file.

Using $psise.CurrentFile in the Windows PowerShell ISE.
Using $psise.CurrentFile in the Windows PowerShell ISE. (Image: Jeff Hicks)

The file object has two methods you can use, Save() and SaveAs(). This means you could programmatically save your file, assuming you have an aversion to Ctrl+S. In the following figure, you can see that this is an untitled file that is most likely empty because IsSaved property is set to True. If I modify the file and check again I will see it is not.

​PS C:Scripts> $psise.CurrentFile | select Fullpath,IsSaved
FullPath IsSaved
 -------- -------
 C:ScriptsUntitled2.ps1 False

Because this is an untitled file, I need to use SaveAs().

​PS C:Scripts> $psise.CurrentFile.saveas("C:workMyscript.ps1")

After I make more changes, then I can use the Save() method.

​PS C:Scripts> $psise.CurrentFile.save()

But what’s the big deal if I can use the menu or keyboard shortcuts? Well, how about a quick way to save all files that need to be saved? The CurrentPowerShellTab property has a property called Files, which is a collection of ISE File objects.

Using $psise.CurrentPowerShellTab.files in the Windows PowerShell ISE.
Using $psise.CurrentPowerShellTab.files in the Windows PowerShell ISE. (Image: Jeff Hicks)

Using PowerShell you can filter the list of files that are titled and unsaved to save them. This is a one-line PowerShell command that will work in PowerShell v4 and later versions.

​$psise.CurrentPowerShellTab.Files.Where({-Not $_.isSaved -AND -Not $_.IsUntitled}).Foreach({$_.save()})

In PowerShell v3, you would need to use the Where-Object and ForEach-Object cmdlets.

​$psise.CurrentPowerShellTab.Files |
 Where-Object {-Not $_.isSaved -AND -Not $_.IsUntitled} |
 ForEach-Object {$_.save()}

All you need to do is wrap this up in a function, assign an alias, and from the command prompt you can quickly save all open files.

​#requires –version 4.O
Function Save-AllISE {
$psise.CurrentPowerShellTab.Files.Where({-Not $_.isSaved -AND -Not $_.IsUntitled}).Foreach({$_.save()}) }
<#
v3
$psise.CurrentPowerShellTab.Files | Where-Object {-Not $_.isSaved -AND -Not $_.IsUntitled} | ForEach-Object {$_.save()}
#>
Set-Alias sa Save-AllISE

Put this in your PowerShell ISE profile and it is trivial to save all open files.

​PS C:Scripts> sa

This only works for files in the current PowerShell tab. If you have other tabs open, then you would need to switch to them and save those files as well. Eventually you will learn another way to use this function.

We’ve just scratched the surface with the PSISE object model. Once you spend some time exploring it with Get-Member, you’ll realize that it isn’t that difficult with a little clever scripting to create shortcuts and tools to get even more accomplished in the PowerShell ISE with less time. If that still seems a bit daunting, don’t fear. I will have more ISE scripts and tools for you in future articles.

SHARE ARTICLE