Get Command History as File

by Jul 14, 2015

The built-in PowerShell ISE editor that ships with PowerShell 3.0 or better can be customized, and you can add your own menu items.

When you run the following code, you will find a new menu item “Get Command History” in your Add-ons menu that can also be triggered by pressing ALT+C.

The command takes your current command history (the commands you entered interactively in the current ISE session) and copies them to a new PowerShell file. This way, it is really easy to save the results of your interactive session. You can even auto-create PowerShell scripts this way: simply remove all the lines that did not work, and keep the lines that produced the desired results.

#requires -Version 3 
$code = 
    $text = Get-History | 
    Select-Object -ExpandProperty CommandLine |

    $file = $psise.CurrentPowerShellTab.Files.Add()
    $file.Editor.Text = $text

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Get Command History', $code, 'ALT+C')

Twitter This Tip! ReTweet this Tip!