Ok, that is a weird blog post title. I could not think of a better one for this post. Anyway, coming to the subject and as I mentioned earlier, I have several PowerShell ISE addons that I use on a regular basis. I kept writing them whenever I found the need. I have been polishing these addons for sharing here. So, here is another one in the ISE addons series.
If you have used PowerShell console or even traditional console (cmd.exe), you might have used the F7 functionality of the console host. This, when used, displays a nice little pop-up menu with the command history. You can just scroll through the list and select an item to execute it again at the console. This will be very useful when you have very long command history and using arrows keys won’t really help. This is how that popup looks:
However, this functionality is not available in PowerShell ISE. So, here is a simple addon that shows command history in a WinForms data grid and you can double click on the desired item to copy the CommandLine text to PowerShell ISE command pane.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
<span style="color: #00008b;">function</span> <span style="color: #8a2be2;">Show-ISEHistoryDialog</span><span style="color: #000000;">{</span> <span style="color: #006400;">#region form code</span> <span style="color: #ff4500;">$historyFormCode</span> <span style="color: #a9a9a9;">=</span> <span style="color: #8b0000;">@' using System; using System.Windows.Forms; namespace ISEGetHistory { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.Close(); } } partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// ///true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.AllowUserToAddRows = false; this.dataGridView1.AllowUserToDeleteRows = false; this.dataGridView1.AllowUserToResizeColumns = true; this.dataGridView1.Location = new System.Drawing.Point(12, 12); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.ReadOnly = true; this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.dataGridView1.Size = new System.Drawing.Size(525, 214); this.dataGridView1.TabIndex = 0; this.dataGridView1.MultiSelect = false; // // button1 // this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.button1.Location = new System.Drawing.Point(231, 232); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "Close"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.button1; this.ClientSize = new System.Drawing.Size(545, 263); this.ControlBox = false; this.Controls.Add(this.button1); this.Controls.Add(this.dataGridView1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.ShowIcon = false; this.ShowInTaskbar = false; this.Text = "ISEGet-History"; this.TopMost = true; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.Button button1; } } '@</span> <span style="color: #006400;">#endregion</span> <span style="color: #006400;">#region Main UI display</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #a9a9a9;">-not</span> <span style="color: #000000;">(</span><span style="color: #8b0000;">'ISEGetHistory.Form1'</span> <span style="color: #a9a9a9;">-as</span> <span style="color: #008080;">[System.Type]</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">Add-Type</span> <span style="color: #000080;">-ReferencedAssemblies</span> <span style="color: #8b0000;">'System.Windows.Forms'</span><span style="color: #a9a9a9;">,</span><span style="color: #8b0000;">'System.Drawing'</span> <span style="color: #000080;">-TypeDefinition</span> <span style="color: #ff4500;">$historyFormCode</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#Create an instance of the form</span> <span style="color: #ff4500;">$iseHistoryDialog</span> <span style="color: #a9a9a9;">=</span> <span style="color: #0000ff;">New-Object</span> <span style="color: #000080;">-TypeName</span> <span style="color: #8a2be2;">ISEGetHistory.Form1</span> <span style="color: #006400;">#Retrieve the DataGridView Control</span> <span style="color: #ff4500;">$dataGridView</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$iseHistoryDialog</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">Controls</span><span style="color: #a9a9a9;">[</span><span style="color: #8b0000;">"dataGridView1"</span><span style="color: #a9a9a9;">]</span> <span style="color: #ff4500;">$dataGridView</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">AutoSizeColumnsMode</span> <span style="color: #a9a9a9;">=</span> <span style="color: #008080;">[System.Windows.Forms.DataGridViewAutoSizeColumnsMode]</span><span style="color: #a9a9a9;">::</span><span style="color: #000000;">Fill</span> <span style="color: #006400;">#Create and add an eventhandler for the data grid view double-click</span> <span style="color: #ff4500;">$dbClickEventHandler</span> <span style="color: #a9a9a9;">=</span> <span style="color: #000000;">{</span> <span style="color: #ff4500;">$selectedRowIndex</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$dataGridView</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CurrentRow</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">Index</span> <span style="color: #ff4500;">$cmdline</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$script:historyInfo</span><span style="color: #a9a9a9;">[</span><span style="color: #ff4500;">$selectedRowIndex</span><span style="color: #a9a9a9;">]</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CommandLine</span> <span style="color: #ff4500;">$iseHistoryDialog</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">DialogResult</span> <span style="color: #a9a9a9;">=</span> <span style="color: #008080;">[System.Windows.Forms.DialogResult]</span><span style="color: #a9a9a9;">::</span><span style="color: #000000;">OK</span> <span style="color: #000000;">}</span> <span style="color: #ff4500;">$dataGridView</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">add_CellDoubleClick</span><span style="color: #000000;">(</span><span style="color: #ff4500;">$dbClickEventHandler</span><span style="color: #000000;">)</span> <span style="color: #006400;">#Define the array list and add all history items</span> <span style="color: #ff4500;">$array</span> <span style="color: #a9a9a9;">=</span> <span style="color: #0000ff;">New-Object</span> <span style="color: #8a2be2;">System.Collections.ArrayList</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$script:historyInfo</span> <span style="color: #a9a9a9;">=</span> <span style="color: #0000ff;">Get-history</span> <span style="color: #a9a9a9;">|</span> <span style="color: #0000ff;">Select</span> <span style="color: #8a2be2;">Id</span><span style="color: #a9a9a9;">,</span><span style="color: #8a2be2;">commandLine</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #ff4500;">$array</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">AddRange</span><span style="color: #000000;">(</span><span style="color: #ff4500;">$script:historyInfo</span><span style="color: #000000;">)</span> <span style="color: #000000;">}</span> <span style="color: #00008b;">else</span> <span style="color: #000000;">{</span> <span style="color: #008080;">[System.Windows.Forms.MessageBox]</span><span style="color: #a9a9a9;">::</span><span style="color: #000000;">Show</span><span style="color: #000000;">(</span><span style="color: #8b0000;">"Nothing in the history"</span><span style="color: #000000;">)</span> <span style="color: #a9a9a9;">|</span> <span style="color: #0000ff;">Out-Null</span> <span style="color: #00008b;">return</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#add the data source to data grid view</span> <span style="color: #ff4500;">$dataGridView</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">DataSource</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$array</span> <span style="color: #006400;">#Show the dialog</span> <span style="color: #ff4500;">$result</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$iseHistoryDialog</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">ShowDialog</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$result</span> <span style="color: #a9a9a9;">-eq</span> <span style="color: #008080;">[System.Windows.Forms.DialogResult]</span><span style="color: #a9a9a9;">::</span><span style="color: #000000;">OK</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #00008b;">return</span> <span style="color: #ff4500;">$cmdLine</span> <span style="color: #000000;">}</span> <span style="color: #00008b;">else</span> <span style="color: #000000;">{</span> <span style="color: #00008b;">return</span> <span style="color: #ff4500;">$false</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$selectedCmdLine</span> <span style="color: #a9a9a9;">=</span> <span style="color: #0000ff;">Show-ISEHistoryDialog</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #006400;">#Let us first clear the CommandPane to make sure there is no stale text</span> <span style="color: #ff4500;">$psise</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CurrentPowerShellTab</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CommandPane</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">Clear</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #006400;">#Now, Insert the selected text</span> <span style="color: #ff4500;">$psise</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CurrentPowerShellTab</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CommandPane</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">InsertText</span><span style="color: #000000;">(</span><span style="color: #ff4500;">$selectedCmdLine</span><span style="color: #000000;">)</span> <span style="color: #006400;">#And, ensure that we focus the commandPane</span> <span style="color: #ff4500;">$psise</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CurrentPowerShellTab</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">CommandPane</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">Focus</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #000000;">}</span> |
Now, I just used my ISE Addon menu generator to add this scriptblock to ISE as an addon.
This is it. Now, I can press F7 in PowerShell ISE and get a list of all commands in the command history.
Now, I can just select an item in that list and double-click. It will appear at the PowerShell ISE command pane. I just need to press enter. 🙂