Tag Archives: Out-Gridview

What PowerShell Script Was I Working On?

Last week I shared a script for finding recently modified files in a given directory. In fact, it wouldn’t be that difficult to find the last files I was working on and open them in the PowerShell ISE. Assuming my Get-RecentFile function is loaded it is a simple as this:

But often, I already have the PowerShell ISE open. True, I could use the same expression substituting ‘psedit’ for ‘ise’, but with the ISE I’m all about working quickly So I have another function targeted for the PowerShell ISE called Open-LastScript.

The core commands in this function are very similar to my other script. And I probably could get by with one script so consider Open-LastScript as a variation on a theme. The function gets PowerShell related files from my Scripts directory. I could have hard coded a default value, but instead I set it to a global variable called ScriptPath which I define in my PowerShell ISE profile script. If that variable isn’t defined, or if nothing is specified for –Path, then the function uses the current location, assuming it is a FileSystem path.

But the fun part, at least for me, is that I take the results of the DIR command and create a few custom properties, including one that shows the number of lines in the script file.

The selected objects and properties are then piped to Out-Gridview. Starting in PowerShell 3.0 you can pass objects back to the pipeline from Out-Gridview. In other words, you can use Out-Gridview as a simple object picker. The function by default gets the 10 most recently modified scripts.

I can select as many entries as I want and click OK. Each selected script will then be opened in the ISE. Yes, I know I can see similar information in the most recently used list but that can show files outside of my Scripts folder and I’m limited to maximum MRU count. My function lets me specify the number of files to list.

The bottom line is that my Open-LastScript function is a handy tool that saves me a little bit of time finding the files I need to work on and every little bit helps. I could create an Add-Ons menu shortcut to run the function with the defaults, but for some reason I opted for an alias.

The alias gives me a little bit of flexibility.

I hope this gives you some ideas of how you can use PowerShell to make your scripting more efficient, or at the very least as an example of PowerShell scripting techniques.

Browsing PowerShell Commands

Whenever I’m exploring a new PowerShell module or snapin, one of the first things I do is list all of the commands found within the module.

You can specify either a module or a snapin. Use the -module parameter for both. However, for larger modules, I’ve realized I need a better way to browse the commands. For example, I might need to see them organized by verb or noun. That information is included with the Get-Command expression. I simply have to ask for it. Here’s a more thorough command and the result.


I included the command type because some modules might contains cmdlets and functions. I could revise this expression and insert a sort command. But that’s too much typing. Especially if I want to sort and re-sort. Instead, I’ll pipe this command to Out-Gridview.


Now I have a sortable and filterable view of all the commands. Plus, it is in a separate window so I have my prompt back and get help for listed commands. I even decided to build a quick-and-dirty function.

Because the module or snapin name can include a wildcard, I added the module name to the output. Now I have a tool to grab all the commands from a module or set of modules and I can filter and browse all I want without having to retype or revise commands at the prompt.

Friday Fun: View Objects in a PowerShell GridList

One of the things that makes PowerShell easy to learn is discoverability. Want to know more about a particular type of object? Pipe it to Get-Member. Or if you want to see values pipe it to Select-Object.

That’s not too bad. Or you can pipe to Out-Gridview.

However, for a single object it isn’t easy to see everything .

What would be easier, at least for me, is to use Out-Gridview but see the properties in a list like I can with Select-Object. So I wrote an advanced function to do just that. The function takes an incoming object, creates a hashtable based on the object’s properties and then displays the hashtable in Out-Gridview.

The function will accept multiple objects, which is why I have a data array to handle multiple piped in objects. But it really works best with a single object. In the Process scriptblock I pass the object to Get-Member to retrieve properties. Then the function creates a hashtable where the key is each property and the value is the corresponding value from the object.

At this point I could simple write the hashtable to Out-Gridview, but the column headings would say Name and Value. It’s a small detail but I use a hashtable with Select-Object to rename “Name” to “Property”. By the way, you may have noticed this line.

That isn’t a typo. If I pipe multiple objects, then $data will be an array of hashtables and so I need to get an enumerator for each one. But with this function now I get the display I want.


There really isn’t any filtering to do but you can click on the column headings to sort. Or your can run Out-Gridlist with -passthru so you can select properties and pass them through to the pipeline.

I hope you find this useful. Enjoy responsibly.