When I’m doing development, I tend to open several PowerShell windows at the same time. For example: I will have one window on my code directory to use run git/svn commands, another in my code generation or build directory to rebuild the code as necessary, another where I run commands while doing manual testing of what I’m working on and so forth.
Console2 makes this a lot nicer as I can open each PowerShell instance in a different tab, which makes it a lot easier to manage. The annoying part, however, comes when I need to switch Virtual Machines to temporarily work on other projects.
For those I will also need several PowerShell windows in different places, and it’s normal for me to switch 2 or 3 times a day between virtual machines. It’s very inconvenient to have to manually recreate my PowerShell windows each time I’m going to do something: Remembering the directory each window needs is not a problem, except for how long the paths are at times, but having to remember which commands I ran on each one can be very bothersome (particularly during testing).
To work around this, I created a very simple PowerShell script to allow me to persist an existing PowerShell session and reload it later when I need to start working again. Here’s the script:
#
# Simple script to save and restore powershell sessions
# Our concept of session is simple and only considers:
# - history
# - The current directory
#
function script:get-sessionfile([string] $sessionName) {
return "$([io.path]::GetTempPath())$sessionName";
}
function export-session {
param ([string] $sessionName = "session-$(get-date -f yyyyMMddhh)")
$file = (get-sessionfile $sessionName)
(pwd).Path > "$file-pwd.ps1session"
get-history | export-csv "$file-hist.ps1session"
"Session $sessionName saved"
}
function import-session([string] $sessionName) {
$file = (get-sessionfile $sessionName)
if ( -not [io.file]::Exists("$file-pwd.ps1session") ) {
write-error "Session file doesn't exist"
} else {
cd (gc "$file-pwd.ps1session")
import-csv "$file-hist.ps1session" | add-history
}
}
As you can see, it’s a trivial script. My concept of a session is very limited in scope, as it contains only the command history and the current directory, which are saved on two files on my temp directory.
I could easily extend it to persist the entire default directory stack instead of just the current location, but avoiding having to switch directories all the time is the reason I keep multiple windows open at the same time.
This is a rather low-tech solution to my needs, but it works rather well for me :-).