Home PowerShell Internals ISE Editor Managing ISE MRU List
 

Managing ISE MRU List

PowerShell ISE exposes a lot of its functionality through $PsISE. With this variable, you can open files and with a little-known trick, close them, too. There are plenty of use cases. One is to set the list of most recently used files (MRU List) in the ISE main menu.

Note that $PsISE is available only when you run a PowerShell script inside the ISE editor and that this article assumes that you are using PowerShell 3.0 although a lot of this worked already in PowerShell 2.0.

Clearing MRU List

To quickly clear your MRU list, you can use this approach:

  

$count = $psise.Options.MruCount
$psise.Options.MruCount = 0
$psise.Options.MruCount = $count

  Clear the ISE MRU List

Adding Paths to MRU List

To add new files programmatically to the MRU list, you can temporarily open them in ISE:

  

$psise.CurrentPowerShellTab.Files.Add('c:\test.ps1')

  Adding a new path to the MRU List

Which raises the question how PowerShell would close the added file. Turns out there is no way. However, what you can do is add a new PowerShell tab, add the temporary files to this tab, then clear its file collection and remove the tab.

Here is the complete code for two useful ISE functions:

  

<#
  .SYNOPSIS
  clears the MRU list in the ISE "file" menu
  .EXAMPLE
  Clear-ISEMRUList
  empties the MRU list
#>
Function Clear-ISEMRUList
{
  $count = $psise.Options.MruCount
  $psise.Options.MruCount = 0
  $psise.Options.MruCount = $count
}

<#
  .SYNOPSIS
  adds new files to the ISE mru list in its "file" menu. The files must exist.
  .EXAMPLE
  Add-ISEMRUList c:\testscript.ps1
  adds the path c:\testscript.ps1 to the existing entries in the ISE MRU list
  .EXAMPLE
  Get-ChildItem c:\myscripts *.ps1 | Add-ISEMRUList
  adds the path of all powershell scripts in the folder c:\myscripts to the ISE MRU list.
  If there are more files than MruCount allows, only the first files are added that have room in the MRU list.
#>
Function Add-ISEMRUList
{
  param
  (
    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
    [Alias('FullName')]
    [String[]]$Path
  )
 
  Begin
  {
    $tab = $psise.PowerShellTabs.Add()
    $max = $psise.Options.MruCount
    $current = 0
  }
 
  Process
  {
    $Path |
    ForEach-Object {
      $path = $_
      try {
        if ($Current -lt $max)
        {
          $null = $tab.Files.Add($path)
          $current++
        }
      }
      catch
      { Write-Warning "'$path' not added: $_"}
    }
  }
 
  End
  {
    $tab.files.Clear()
    $psise.PowerShellTabs.Remove($tab)
  }
}

  Clear- and Add-ISEMRUList

Script: Manage ISE MRU List
Script: Manage ISE MRU List
PTSManageISEMRU.ps1
Version: 1.0
1.4 KiB
1022 Downloads
Details...