LIVE NOW: GET-IT: Endpoint Management 1-Day Conference LIVE NOW: GET-IT: Endpoint Management 1-Day Conference

An Overview of the PowerShell ISE Object Model

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.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

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> $

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({$})

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 {$}

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({$}) } 
$psise.CurrentPowerShellTab.Files | Where-Object {-Not $_.isSaved -AND -Not $_.IsUntitled} | ForEach-Object {$} 

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.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: