1

How can I configure PowerShell ISE so that it will open tabs for each file that was previously opened when I closed it?

This is particularly helpful since I typically come back to work on the same script or set of scripts.

0

Unfortunately, I couldn't get this to work automatically/on a timer. It uses the $psISE variable which I can't figure out how to access via background jobs or registered events.

You need to edit your ISE profile, you may need to create it and it usually lives in C:\Users_USERNAME_\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

First, specify where to save the info about what you had open:

$SavePath = Join-Path -Path (Split-Path -Path $profile) -ChildPath 'openFiles.txt'

That will end up going in a txt file in the same folder as the profile.

Then check for the files existence, and open the files listed in it.

if (Test-Path -Path $SavePath)
{
    $OpenFiles = Import-Csv -Path $SavePath
    psEdit -filenames $OpenFiles
}

The final piece of the puzzle is a function that grabs the current open files, and dump the full paths to them into the txt file.

function Save-OpenFile {
    $Files = @()

    foreach ($Tab in $psISE.PowerShellTabs)
    {
        foreach ($File in ($Tab.Files | Where-Object { !$_.IsUntitled }))
        {
            $Files += $File.FullPath
        }
    }

    $Files | Out-File -FilePath $SavePath
}

Run Save-OpenFile once in a while (and before closing the ISE) and what you had open at that point in time will re-open. Like I said at the start of this post, it would be great if you could automatically trigger this when the ISE closes but I couldn't access the info within an event like that.

You could simplify things by adding an alias for the function, or doing something else to add it to your regular workflow so that it becomes habit that your running it regularly.

Hope this helps or starts you on a track to getting a full solution.

Note: Some alternative PowerShell editors will re-open your documents between sessions, so if you're dead set on it you may want to investigate those as options.

  • Which alternative PowerShell editors offer this functionality? – Ryan Gates Jul 2 '15 at 16:47
  • @RyanGates, I think PowerGUI. It was part of the standard build on my workstation but I don't personally use it. Failing that try PrimalScript/PowerShell Studio. – Windos Jul 2 '15 at 19:55
0

You could also run:

Stop-Process $pid -Force

This will kill the process for the ISE and the next time you run the ISE it should prompt to reload the last tabs.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.