├── example.gif ├── LICENSE ├── README.md ├── Restore.psm1 └── restore.psd1 /example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ironmansoftware/restore/HEAD/example.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ironman Software, LLC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Restore 2 | 3 | Restore saves the state of your terminal so you can restore it the next time you open one. 4 | 5 | ![](./example.gif) 6 | 7 | ## Disclaimer 8 | 9 | This is probably not a great idea. Every time you execute a command, this module serializes your location, variables and loaded modules to a CLIXML file. 10 | 11 | ## How to use 12 | 13 | Install the module. 14 | 15 | ``` 16 | Install-Module Restore 17 | ``` 18 | 19 | Add it to your profile. 20 | 21 | ``` 22 | Import-Module Restore 23 | ``` 24 | 25 | Restore terminals that you closed previously. 26 | 27 | ``` 28 | Restore-Terminal 29 | ``` 30 | 31 | After restoring a terminal, the following will be restored. 32 | 33 | - Location 34 | - Variables 35 | - Modules 36 | 37 | ## How it works 38 | 39 | If you want to use this module, load it into your PowerShell profile. 40 | 41 | ``` 42 | Import-Module Restore 43 | ``` 44 | 45 | When you import the module, it assigns an event handler to the `AvailabilityChanged` event of the main runspace. 46 | 47 | ``` 48 | $Host.Runspace.add_AvailabilityChanged({Checkpoint-Terminal}) 49 | ``` 50 | 51 | Every time the runspace changes state (like when it completes running a command), it will call `Checkpoint-Terminal`. This will create a hashtable with your session state, serialize it to CLIXML and save it to disk. 52 | 53 | ``` 54 | $state = @{ 55 | Location = (Get-Location).ToString() 56 | Variables = Get-Variable -Scope "Global" | ForEach-Object { [PSCustomObject]@{ Name = $_.Name; Value = $_.Value } } 57 | Modules = Get-Module | ForEach-Object { [PSCustomObject]@{ Name = $_.Name; Path = $_.Path } } 58 | } 59 | 60 | Start-Job -ScriptBlock { 61 | $args[0] | Export-Clixml -Path "$Env:AppData\restore.$pid.clixml" 62 | } -ArgumentList $state 63 | ``` 64 | 65 | If you close a terminal that you didn't mean to close, you can call `Restore-Terminal` in a new PowerShell window to load up the terminal state. Kinda like `Ctrl+Shift+T` in a browser. -------------------------------------------------------------------------------- /Restore.psm1: -------------------------------------------------------------------------------- 1 | function Restore-Terminal { 2 | [CmdletBinding()]param() 3 | 4 | $Restore = Get-ChildItem "$Env:APPDATA\restore.*.clixml" | Where-Object { 5 | $process = Get-Process -Id ($_.Name.Split('.')[1]) -ErrorAction SilentlyContinue 6 | -not $_.FullName.Contains($Pid.ToString()) -and $null -eq $process 7 | } | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 8 | if (-not $Restore) 9 | { 10 | return 11 | } 12 | 13 | $Terminal = Import-Clixml -Path $Restore.FullName 14 | 15 | Write-Verbose "Restoring from checkpoint: $($Restore.FullName)" 16 | Write-Verbose "Restoring terminal location: $($Terminal.Location)" 17 | 18 | Remove-Item $Restore.FullName 19 | 20 | Set-Location $Terminal.Location 21 | $Terminal.Variables | ForEach-Object { 22 | try { 23 | Set-Variable -Name $_.Name -Value $_.Value -ErrorAction SilentlyContinue -Scope "Global" 24 | } catch {} 25 | } 26 | $Terminal.Modules | ForEach-Object { 27 | Import-Module $_.Path -Scope "Global" 28 | } 29 | } 30 | 31 | function Clear-TerminalCheckpoint { 32 | $Checkpoints = Get-ChildItem "$Env:APPDATA\restore.*.clixml" 33 | 34 | if ($Checkpoints.Length -gt 10) { 35 | $Checkpoints | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 10 | Remove-Item 36 | } 37 | } 38 | 39 | function Checkpoint-Terminal { 40 | $state = @{ 41 | Location = (Get-Location).ToString() 42 | Variables = Get-Variable -Scope "Global" | ForEach-Object { [PSCustomObject]@{ Name = $_.Name; Value = $_.Value } } 43 | Modules = Get-Module | ForEach-Object { [PSCustomObject]@{ Name = $_.Name; Path = $_.Path } } 44 | } 45 | 46 | Start-Job -ScriptBlock { 47 | $args[0] | Export-Clixml -Path "$Env:AppData\restore.$pid.clixml" 48 | } -ArgumentList $state 49 | } 50 | 51 | $Host.Runspace.add_AvailabilityChanged({Checkpoint-Terminal}) 52 | Clear-TerminalCheckpoint -------------------------------------------------------------------------------- /restore.psd1: -------------------------------------------------------------------------------- 1 | # 2 | # Module manifest for module 'restore' 3 | # 4 | # Generated by: Adam Driscoll 5 | # 6 | # Generated on: 27/09/2020 7 | # 8 | 9 | @{ 10 | 11 | # Script module or binary module file associated with this manifest. 12 | RootModule = 'restore.psm1' 13 | 14 | # Version number of this module. 15 | ModuleVersion = '0.0.1' 16 | 17 | # Supported PSEditions 18 | # CompatiblePSEditions = @() 19 | 20 | # ID used to uniquely identify this module 21 | GUID = '4d0a568b-d7d6-45b2-8ba7-5aa90ead3827' 22 | 23 | # Author of this module 24 | Author = 'Adam Driscoll' 25 | 26 | # Company or vendor of this module 27 | CompanyName = 'Unknown' 28 | 29 | # Copyright statement for this module 30 | Copyright = '(c) Adam Driscoll. All rights reserved.' 31 | 32 | # Description of the functionality provided by this module 33 | Description = 'Restore terminal state.' 34 | 35 | # Minimum version of the PowerShell engine required by this module 36 | # PowerShellVersion = '' 37 | 38 | # Name of the PowerShell host required by this module 39 | # PowerShellHostName = '' 40 | 41 | # Minimum version of the PowerShell host required by this module 42 | # PowerShellHostVersion = '' 43 | 44 | # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 45 | # DotNetFrameworkVersion = '' 46 | 47 | # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 48 | # ClrVersion = '' 49 | 50 | # Processor architecture (None, X86, Amd64) required by this module 51 | # ProcessorArchitecture = '' 52 | 53 | # Modules that must be imported into the global environment prior to importing this module 54 | # RequiredModules = @() 55 | 56 | # Assemblies that must be loaded prior to importing this module 57 | # RequiredAssemblies = @() 58 | 59 | # Script files (.ps1) that are run in the caller's environment prior to importing this module. 60 | # ScriptsToProcess = @() 61 | 62 | # Type files (.ps1xml) to be loaded when importing this module 63 | # TypesToProcess = @() 64 | 65 | # Format files (.ps1xml) to be loaded when importing this module 66 | # FormatsToProcess = @() 67 | 68 | # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess 69 | # NestedModules = @() 70 | 71 | # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. 72 | FunctionsToExport = 'Restore-Terminal', 'Clear-TerminalCheckpoint', 'Checkpoint-Terminal' 73 | 74 | # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. 75 | CmdletsToExport = @() 76 | 77 | # Variables to export from this module 78 | VariablesToExport = @() 79 | 80 | # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. 81 | AliasesToExport = @() 82 | 83 | # DSC resources to export from this module 84 | # DscResourcesToExport = @() 85 | 86 | # List of all modules packaged with this module 87 | # ModuleList = @() 88 | 89 | # List of all files packaged with this module 90 | # FileList = @() 91 | 92 | # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. 93 | PrivateData = @{ 94 | 95 | PSData = @{ 96 | 97 | # Tags applied to this module. These help with module discovery in online galleries. 98 | Tags = @('productivity') 99 | 100 | # A URL to the license for this module. 101 | LicenseUri = 'https://github.com/ironmansoftware/restore/blob/master/LICENSE' 102 | 103 | # A URL to the main website for this project. 104 | ProjectUri = 'https://github.com/ironmansoftware/restore' 105 | 106 | # A URL to an icon representing this module. 107 | # IconUri = '' 108 | 109 | # ReleaseNotes of this module 110 | # ReleaseNotes = '' 111 | 112 | # Prerelease string of this module 113 | # Prerelease = '' 114 | 115 | # Flag to indicate whether the module requires explicit user acceptance for install/update/save 116 | # RequireLicenseAcceptance = $false 117 | 118 | # External dependent modules of this module 119 | # ExternalModuleDependencies = @() 120 | 121 | } # End of PSData hashtable 122 | 123 | } # End of PrivateData hashtable 124 | 125 | # HelpInfo URI of this module 126 | # HelpInfoURI = '' 127 | 128 | # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. 129 | # DefaultCommandPrefix = '' 130 | 131 | } 132 | 133 | --------------------------------------------------------------------------------