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.

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on September 15, 2010, in WMF (Powershell/WinRM) and tagged , . Bookmark the permalink. 3 Comments.

  1. Seems not all items which are able to see from F7 window can be get by get-history: only some truncated portion is pullable.

  2. all it does is take the output of the Get-History command and dump it in the list box. so it should have the exact same output as manually running get-history

  3. Great function, although Powershell v3 throws up a couple of errors, firstly on the ListBox backcolor line (I just hardcoded in an RGB value, eg. $objlb.backcolor = “255,255,255”)

    The second and biggest issue is with the lines that copy the chosen item onto the Command Window as this no longer works! It appears PS v3 now uses a different command to reference this. The corrected code should be:

    $objlb.Add_DoubleClick({$psISE.CurrentPowerShellTab.ConsolePane.InputText = $objlb.Selecteditem;$objForm.Close()})

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: