none
Reopen last used tabs in Powershell ISE RRS feed

  • Question

  • Hi Scripting Guys

    I use a lot of tabs in Powershell ISE console in my work, as an Administrator, and every time I close the ISE I need to reopen every tabs that I used before. So my question is:

    When I open Powershell ISE can it automatically reopen last tabs, that I used? Can Powershell ISE remember that last used tabs like IE can remember last open browser session?

    Best regards,

    Thorkell

    Sunday, April 6, 2014 3:15 PM

Answers

  • This really isn't a scripting question but a "how do I make the ISE work the way I want" question.

    The answer is that the ISE doesn't have this feature built-in, and killing it unceremoniously is a workaround.

    Another alternative is a different IDE/editor that knows how to save state.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 8, 2014 3:15 PM
    Moderator

All replies

  • NO but you will likely find an add-in that can do that.  Have you tried searching?

    You can alos write your own.  The UI is completely documented.

    This took me less the 15 seconds to find using that new invention called a "search engine".  It is a really cool gadget that you can get with most browsers.

    https://connect.microsoft.com/PowerShell/feedback/details/790578/windows-powershell-ise-add-ability-to-save-load-session-state


    ¯\_(ツ)_/¯

    Sunday, April 6, 2014 5:14 PM
  • Here is a good resource.  It is on the menu in V4.  I am not sure the link is there in previous versions:
    http://social.technet.microsoft.com/wiki/contents/articles/2969.windows-powershell-ise-add-on-tools.aspx

    ¯\_(ツ)_/¯

    Sunday, April 6, 2014 5:28 PM
  • Hi,

    I do this by killing the ISE process with process explorer instead of closing the window. The next time you open the ISE it will note that it didn't shut down properly and will reopen the tabs you had open previously.

    I'm not aware of a graceful way to do this though.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Sunday, April 6, 2014 6:31 PM
  • Mike - I like that.  I thinkthey call that the "nuclear option".  Hey - if it works in Washington why not in virtual space?


    ¯\_(ツ)_/¯

    Sunday, April 6, 2014 6:37 PM
  • Nuke it from orbit, it's the only way to be sure.

    =]


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Sunday, April 6, 2014 6:44 PM
  • Space Invaders ... best game in history.

    ¯\_(ツ)_/¯

    Sunday, April 6, 2014 6:47 PM
  • Hi

    I have tried to search for the option and have had no luck. But I will look at this link that you mentioned

    Thanks for the tips

    Thorkell

    Monday, April 7, 2014 3:31 PM
  • Hi

    In way that can be done, but that is the brutal way :) Strange though that Microsoft have not implement itinto the Powershell ISE, since we are living in the 21st century and Microsoft have the power to do so. Would be interesting to hear the argument why the did not implemented in the newest version of Powershell.

    br

    Thorkell

    Monday, April 7, 2014 3:36 PM
  • In way that can be done, but that is the brutal way :)

    Yep, indeed it is. =]

    Would be interesting to hear the argument why the did not implemented in the newest version of Powershell.

    I completely agree, I don't understand why this feature doesn't exist. Especially since I know for a fact that the ISE is capable of reopening tabs if you force it to.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Monday, April 7, 2014 3:40 PM
  • Hi

    In way that can be done, but that is the brutal way :) Strange though that Microsoft have not implement itinto the Powershell ISE, since we are living in the 21st century and Microsoft have the power to do so. Would be interesting to hear the argument why the did not implemented in the newest version of Powershell.

    br

    Thorkell

    They asked me to do it for them but I did not want to ruin the users fun.  Just think of how boring this day would be if you didn't have this to think about?

    Happy Poshing;)


    ¯\_(ツ)_/¯

    Monday, April 7, 2014 4:46 PM
  • Hi,

    I do this by killing the ISE process with process explorer instead of closing the window. The next time you open the ISE it will note that it didn't shut down properly and will reopen the tabs you had open previously.

    I'm not aware of a graceful way to do this though.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)


    kill $PID -Force

    I've done this a few times to keep my tabs when I open up ISE another time. :)

    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book


    Monday, April 7, 2014 5:48 PM
    Moderator
  • kill $PID -Force

    I've done this a few times to keep my tabs when I open up ISE another time. :)

    Nice, that's pretty simple. I forgot about $PID.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Monday, April 7, 2014 6:02 PM
  • Hi

    Ran this code, with 4 tabs and 1 tab then I added a new tab . Then open the program again and PS ISE didn't display the last open tabs. Then open again PS ISE with 4 tabs and the ended the process via Task Manager (Process Explorer) with a same result

    The version of PS ISE that I am running is v3. can that be the problem ?

    Tuesday, April 8, 2014 3:08 PM
  • The ISE only saves its state every so often, so the kill method isn't foolproof.

    I think the important value here is the 'AutoSave interval...' option in the ISE General Settings tab.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Tuesday, April 8, 2014 3:11 PM
  • This really isn't a scripting question but a "how do I make the ISE work the way I want" question.

    The answer is that the ISE doesn't have this feature built-in, and killing it unceremoniously is a workaround.

    Another alternative is a different IDE/editor that knows how to save state.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 8, 2014 3:15 PM
    Moderator
  • So does anyone know the code that Powershell uses to save its state? We could add that to a shutdown script then the problem would be solved.
    Monday, April 21, 2014 5:46 PM
  • I don't think that will solve the problem, because a graceful shutdown removes the saved state, doesn't it?

    The real answer is that the ISE doesn't support this feature without add-on code. Killing the process is a brute-force workaround.

    Another alternative is to use a different IDE that supports this feature.


    -- Bill Stewart [Bill_Stewart]


    Monday, April 21, 2014 8:56 PM
    Moderator
  • So does anyone know the code that Powershell uses to save its state? We could add that to a shutdown script then the problem would be solved.

    You are free to write an add-in for PowerShell that will save the current tabs and exit.  You can reload in the profile.  You can also look in the repository and search as there are many people who have already done this.


    ¯\_(ツ)_/¯

    Monday, April 21, 2014 9:52 PM
  • Here is an event that you can use to save the state on an exit.  It is touchy and will not allow output to the screen.  It can read and output the sate.


    ¯\_(ツ)_/¯

    Monday, April 21, 2014 9:58 PM
  • old thread, current issue.

    I added a profile in $profile and put some lines of code in.
    You can copy the code below.
    F6 will save all files. (modify the code if you want different behavior for untitled files)
    It still contains unused parts I am still playing with.

    This will allow me to save all tab contents and automatically restore them on load.
    Not perfect, but does the basic trick.
    I played with events as well, but "Exiting" gets fired when the tabs already are closed, so the $psISE.PowerShellTabs variable is empty.
    I tried to automatically safe the state with "OnIdle". That did work, but it still runs when the ISE is exiting and then randomly empties my state file.

    I am using a csv file to save tab names and files, because I'm old and know what I do there ;)

    There still is an issue with the first tab that gets auto-created on each ISE start. I just left it.

    btw, to rename your tabs to something meaningful select the tab in question, and enter this at the command prompt:
    $psISE.CurrentPowerShellTab.DisplayName="foo"
    These names will be restored with my script.

    Have fun, comments welcome.

    function saveISEtabs_alt() {
        $profile=get-item $profile
        $csvFile=(join-path (split-path $profile) allISEtabsInfo.csv)
        $allISEtabsInfo=@()
    
        $ts=$psISE.PowerShellTabs
        for ($ti=0; $ti -lt $ts.count; $ti++) {
            $tc=$ts[$ti]    
            $fs=$tc.files
            for ($fi=0; $fi -lt $fs.count; $fi++) {
                $fc=$fs[$fi]
                if ($fc.IsUntitled) {
                    #[void]$fc.SaveAs($fc.FullPath)
                } else {
                    #[void]$fc.Save
                }
            }
            $obj=[PSCustomObject]@{
                DisplayName=$tc.Displayname
                Files=$tc.files.fullpath -join '|'
            }
            $allISEtabsInfo+=$obj
        }
    
        $allISEtabsInfo | export-csv -NoTypeInformation $csvFile
    }
    
    
    function saveISEtabs() {
        $profile=get-item $profile
        $csvFile=(join-path (split-path $profile) allISEtabsInfo.csv)
        $allISEtabsInfo=@()
    
        $psISE.PowerShellTabs | %{
    
            $_.files | %{
                if (!$_.IsSaved) {
                    [void]$_.SaveAs($_.FullPath)
                } else {
                    [void]$_.Save
                }
            }
        
            
            $obj=[PSCustomObject]@{
                DisplayName=$_.Displayname
                Files=$_.files.fullpath -join '|'
            }
            $allISEtabsInfo+=$obj
        }
        $allISEtabsInfo | export-csv -NoTypeInformation $csvFile
    }
    
    
    function loadISEtabs() {
        $profile=get-item $profile
        $csvFile=(join-path (split-path $profile) allISEtabsInfo.csv)
        $allISEtabsInfo=Import-Csv $csvFile
    
        $allISEtabsInfo | %{
    
            if ($psISE.PowerShellTabs.Displayname -notcontains $_.DisplayName) {
                $newTab=$psISE.PowerShellTabs.Add()
                $newTab.DisplayName=$_.DisplayName
                $_.files.split('|') |%{
                    $newFile=$newTab.files.Add($_) 
                    $newfile
                }
            }
        }
    }
    
    
    
    loadISEtabs
    if (!($psISE.CurrentPowerShellTab.AddOnsMenu.Submenus | Where-Object { $_.DisplayName -eq "Save All" }))
    {
       $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Save All",{saveISEtabs},"F6")
    }
    #$psISE.PowerShellTabs.Remove($psISE.PowerShellTabs[0])
    
    
    <#
    $exit=0
    
    Register-EngineEvent PowerShell.Exiting -SupportEvent -Action `
    {
        $exit=1
    }
    
    Register-EngineEvent PowerShell.OnIdle -SupportEvent -Action `
    {
        if (!$exit) {
            if ($psISE.PowerShellTabs.files.count -ne $bak) {
                saveISEtabs_alt
                $bak=$psISE.PowerShellTabs.files.count
            }
        }
    }
    #>


    Friday, September 21, 2018 5:54 AM
  • Look in the PowerShellGet Gallery as there are now extension modules that will do this.

    find-module -Tag ISE


    \_(ツ)_/

    Friday, September 21, 2018 6:00 AM
  • This took me less the 15 seconds to find using that new invention called a "search engine".  It is a really cool gadget that you can get with most browsers.

    This is rude and unconstructive.
    Friday, October 5, 2018 2:50 PM
  • this is awesome!  I didn't know about the EnvineEvent cmdlets until this post.

    thanks for this.

    Friday, October 5, 2018 2:51 PM
  • Space Invaders?  Isn't that a quote from "Aliens"?
    Wednesday, November 14, 2018 11:42 PM