Friday Fun: Save All Open PowerShell ISE Files

Here’s a little tidbit that I previously shared on Twitter and Google Plus. The PowerShell ISE obviously has a Save menu choice. But there’s no menu option to save all open files. But you can add one yourself. All of the open files are part of the $psise.CurrentPowerShellTab.Files collection. Each item has a Save() method so to save all the files all you need to do is enumerate the collection ForEach and invoke the Save() method. You could run a command like this in the command prompt of the ISE.

$psise.CurrentPowerShellTab.files |  foreach {
    $_.Save()
  }

Where you will run into problems is with files that have not been saved yet and are still untitled. This code will throw exceptions. The solution is to simply skip untitled files.

$psise.CurrentPowerShellTab.files |  
where {-Not ($_.IsUntitled)} |
foreach {
    $_.Save()
  }

You can save untitled files with the SaveAs() method but you have to provide a file name. I can’t find anyway to easily invoke the GUI prompt so for now I have manually save untitled files.

Now, instead of running this command every time, let’s add it to the ISE Add-Ons menu.

$sb={
  $psise.CurrentPowerShellTab.files |
  where {-Not ($_.IsUntitled)} |
  foreach {
    $_.Save()
  }
}

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Save All Files",$sb,"Ctrl+Shift+A") | out-null

I even added a keyboard shortcut. Add these lines to your PowerShell ISE profile and you’ll always have this menu option. Or download Add-SaveAllISE.ps1 and load the script in your profile. As far as I can tell this works in both PowerShell v2 and v3.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati


About the Author

PowerShell.org Announcer

This is the official account for PowerShell.org and sponsor announcements.