Blog Archives

Powershell ISE History Window (why no F7 for ISE?!)


So with a classic command prompt and powershell console you can hit F7 to bring up a history window that will allow you to cycle through past commands easily. Sure you can use the Up and Down key there as well as with ISE but there is no history window for ISE. With all the cool things they’ve done with it, I’m kind of surprised.

So, I’ve come up with a simple little function to create this feature..

function Copy-HistoryItem{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$fg = $psise.Options.OutputPaneForegroundColor
$bg = $psise.Options.OutputPaneBackgroundColor
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "History"
$objForm.Size = New-Object System.Drawing.Size(300,300)
$objForm.StartPosition = "CenterScreen"
$objForm.FormBorderStyle = "SizableToolWindow"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$psISE.CurrentPowerShellTab.CommandPane.InsertText($objlb.Selecteditem);$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

$objForm.Add_Resize({$objlb.Size = New-Object System.Drawing.Size(($objForm.width-15),$objForm.height) })

$objlb = New-Object System.Windows.Forms.ListBox
$objlb.Location = New-Object System.Drawing.Size(0,0)
$objlb.Size = New-Object System.Drawing.Size(($objForm.width-15),$objForm.height)
$objlb.backcolor= [system.drawing.color]::fromargb($bg.a, $bg.r, $bg.g, $bg.b)
$objlb.forecolor = [system.drawing.color]::fromargb($fg.a, $fg.r, $fg.g, $fg.b)
$objlb.Add_DoubleClick({$psISE.CurrentPowerShellTab.CommandPane.InsertText($objlb.Selecteditem);$objForm.Close()})
$objForm.Controls.Add($objlb)

get-history | foreach {$objlb.items.add($_.commandline)} | out-null

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
}
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(‘Show history’,{Copy-HistoryItem},"F7")

 

This even adds the ability to hit the F7 key to bring up the window.

You can resize the window to your liking and just double click on the item you want and it will place the command in the command window for you.

This was inspired by some code i received by a Community member, Kartek Bielawski, over in the MS Forums for Powershell. Most of the code was taken from a Technet Powershell Tip of the Week post on how to use forms.

Design a site like this with WordPress.com
Get started