Select History Via PowerShell Out-Gridview

Summary: Microsoft Scripting Guy, Ed Wilson, continues his tips and tricks series by talking about select Windows PowerShell commands via Out-GridView.

Microsoft Scripting Guy, Ed Wilson, is here. One of the great things about getting a bunch of Windows PowerShell people together is that they begin to share tips and tricks they have figured out. One reason for this is that Windows PowerShell is highly discoverable. Another reason is that there is a whole lot of Windows PowerShell, and therefore, there is a whole bunch of stuff to discover. Many times (at least for me), the coolest tips and tricks are not something that I have never heard of, but they are new ways of doing things that I am already doing.

An example is the trick for today—using the Out-GridView cmdlet to pick commands from Windows PowerShell history to create an initial script. For me, what is cool about this, is that I already do both elements of the trick. That is, I already pick the command lines from history and I pipeline them to create a script.

I also already use the Out-GridView cmdlet’s –Passthru parameter so that I can cherry pick stuff from the displayed grid and return it to the Windows PowerShell console for further processing. But I never thought about putting the two techniques together. Obviously, Lee Holmes had thought of it, and he shared it with the Windows PowerShell User Group in Charlotte.

Suppose I have a number of Windows PowerShell commands in my command history. I would like to select from them and create a script based upon them. One way I can do this is to use Get-History, redirect the command lines to a file, and then open that file in the Windows PowerShell ISE to edit it. But an easier way is to create a temporary file to hold my script, and then pipe my history to Out-GridView so I can see which commands I want to select. I can then pipe the selected commands to my file and open it in the Windows PowerShell ISE.

First, I need to create a temporary file. Well, I really don’t need to create a temporary file, but it is easier. I could create the script file that I want to use in my script folder, and use that. But that means I need to think of a name and all of that. A temp file is easier to create and use. Here, I create a temp file in the temp folder, and store the path to the file in a variable named $file:

$file = [io.path]::GetTempFileName()

Now I use the Get-History cmdlet to retrieve my command history. But I am only interested in the commandline property, so I can select it directly. This is shown here (using the H variable as an alias for the Get-History cmdlet):


I pipe the returned command lines to the Out-GridView cmdlet, and I specify the –PassThru parameter. This means that Windows PowerShell displays the output in a grid. I can then select the items I want from that grid. The GridView control is shown here:

Image of command output

The selected items return to the Windows PowerShell console. (I will pick them up, and then pass and redirect them to my temp file). The command now looks like the following:

(h).commandline | Out-GridView -PassThru

I now write my selected commands to the temp file. I do this by using the redirection operator. Here is the command as it now stands:

(h).commandline | Out-GridView -PassThru >> $file

I want to open the file in the Windows PowerShell ISE so I can play with the commands, add additional information, or even save the output as a Windows PowerShell script. To do this, all I do is add the call to open the Windows PowerShell ISE and specify my file:

(h).commandline | Out-GridView -PassThru >> $file ; ise $file

Here are my two complete commands:

$file = [io.path]::GetTempFileName()

(h).commandline | Out-GridView -PassThru >> $file ; ise $file

Because the output is not a PS1 type of file, I cannot simply press F-8 and run the script. But I can select the commands in the script pane, and press F-5 to run the selection.

Image of command output

I can also use my Copy-ScriptToNewTab function (from my Windows PowerShell ISE module) and THAT will run directly.

Image of command output

That is all there is to using Out-GridView to select commands via Windows PowerShell history. Tricks and Tips Week will continue tomorrow when I will talk about more way cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (2)

  1. MikeM says:

    As always Ed, great tip. Keep em coming.
    I kinda find the following one easier but but I’ll put this tip in my back pocket as well:

    (h).commandline | clip;ise
    followed by Ctrl-V into the ISE editor pane.


  2. D says:

    "Because the output is not a PS1 type of file, I cannot simply press F-8 and run the script. But I can select the commands in the script pane, and press F-5 to run the selection."

    Wrong way round, no?

Skip to main content