The Out-GridView Cmdlet

The information in this article was written against the Community Technology Preview (CTP) of Windows PowerShell 2.0. This information is subject to change in future releases of Windows PowerShell 2.0.

Out-GridView: Displaying Information in a Data Grid

The Windows PowerShell console window has, as the saying goes, a face only a mother could love. (And even Mom seems a little equivocal about the whole thing.) The command window is great for entering data, and perfectly fine for displaying small chunks of data. However, the moment you have multiple columns of data, and/or several hundred lines of data, well, at that point the console window begins to show its limitations.

Limitations, you say? What kind of limitations?

Well for starters, the console window uses non-proportionally spaced fonts (that is, the lowercase i takes up exactly the same amount of screen real estate as the uppercase M). That makes it difficult to display very much information across the width of the window. Likewise, buffer size limitations can limit the total number of lines of information that you can display. Suppose you run a script that returns 300 lines of output. Depending on your settings, you might be able to see only the last 200 of those lines. What happened to the first 100 lines? Well, those first 100 lines have gone to wherever information goes after being squeezed out of the console window buffer.

In addition, there’s the fact that all the data in the console window is static. Would you like to sort the data in a different way? No problem; just change your command or script and re-run the whole thing. Would you like to search the output for a keyword? No problem; copy the entire output, paste it into Microsoft Word, then use Word’s Find command. Would you like to – well, you get the idea. The console window is OK, but what would really be useful would be an alternate venue for displaying data. You know, like maybe a Microsoft Excel-like grid or something. Boy, that would be nice, wouldn’t it?

Well, guess what? As it turns out, you aren’t the only one who’d like to be able to display data in a grid. If nothing else, the Windows PowerShell team apparently had the exact same idea, which explains why the November, 2007 CTP release of Windows PowerShell 2.0 includes a new cmdlet named Out-GridView. What does the Out-GridView cmdlet do? We had a feeling you were going to ask that.

Important. The Out-GridView cmdlet is automatically installed when you install PowerShell 2.0; however, the cmdlet won’t do anything unless you also have .NET Framework 3.0 installed. If you’re running Windows Vista then you already have .NET Framework 3.0 installed; if you’re not running Windows Vista then you can go to this page for more information.

Oh, and if you aren’t sure which version of the .NET Framework you have installed on your computer, well, don’t worry about it: we’ve already written a script that can help you figure that out.

So What Doesthe Out-GridView Cmdlet Do?

A picture is supposed to be worth a thousand words; let’s put that to the test by showing you what the Out-GridView cmdlet does. To do that, let’s take a peek at a very simple – albeit useful – command that uses Out-GridView. In this command we use the Get-Service cmdlet to retrieve information about all the services installed on a computer; we then display that information in a grid. Here’s the command:

Get-Service | Out-GridView

We told you it was simple: we call Get-Service to retrieve the information, then pipe that information to the Out-GridView cmdlet. Out-GridView takes it from there:

That’s not too bad, is it? Granted, the text in column 2 is bit crammed together; what is the name of that one service? AudioEndpoi, something like that. But, hey, that’s the way it goes, right? After all, even when you run your script in the console window the full service name isn’t fully displayed (depending, of course, on your console window settings):

Status   Name               DisplayName
------   ----               -----------
Running  AeLookupSvc        Application Experience
Stopped  ALG                Application Layer Gateway Service
Running  Appinfo            Application Information
Running  AppMgmt            Application Management
Running  AudioEndpointBu... Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
Running  BFE                Base Filtering Engine
Running  BITS               Background Intelligent Transfer Ser...

You win a few, you lose a few.

As it turns out, however, this is one of those few that you win. One of the cool things about the PowerShell grid is that the columns are resizable. Need column 2 to be a tad bit wider? Then simply click on the separator bar between the Name heading and DisplayName heading and drag that bar to the right to resize the column (yes, just like you would in Excel):

Ah, now we see: it’s the AudioEndpointBuilder service. That’s much better.

Resizable columns; these might not get as much publicity as new PowerShell features like remoting and runspaces, but they just might be every bit as useful.

Sorting Data

Let’s try something else. By default, the Get-Service cmdlet sorts service information on the Name property. That’s fine, but suppose that what we really want to do is sort the service information by the DisplayName property. Again, that’s not a big deal; after all, we could just re-run our Get-Service command, taking care to sort the data by DisplayName before handing it off to Out-GridView.

Or, we could simply click on the DisplayName column header. Click once on a column header and the entire grid will be sorted by that column, in ascending order; click a second time and the grid will be sorted in descending order. Take a look at our grid after we click the DisplayName header:

Keep in mind that you can only sort by a single column. In the example we just showed you, that’s fine: we wanted to sort by a single column. But suppose we want to sort all the services first by status and then by display name? Can we do this directly in the grid? No, unfortunately we can’t. If you want to sort by two or more items then you’ll need to sort the data and then pipe it to Out-GridView, like so:

Get-Service | Sort-Object Status, DisplayName | Out-GridView

Grouping Data

Another cool thing you can do with the PowerShell grid is group the displayed information by any of the column headings; all you have to is right-click one of the headings and choose (Toggle) Show in Groups. For example, if we right-click the Status column and choose Show in Groups we’ll get output similar to this, with the services grouped by Status (the number in parentheses indicates the number of items in each group):

That’s cool. But what if you actually wanted to see the individual services in each group? Hey, no problem; simply click the expand/contract arrow located to the left of each group heading, Click the arrow next to the label Stopped and you should see something similar to this:

Should you someday tire of viewing information by group, well, all you have to do is right-click and select (Toggle) Show in Groups a second time. As the word Toggle suggests, this will now turn grouping off. To turn grouping back on, right click and select (Toggle) Show in Groups a third time. To turn it off – well, we could sit here and toggle grouping on and off all day. But we still have a few more features to cover.

Filtering Data

The PowerShell grid also includes a rudimentary filtering function. To show only those services whose returned data includes a specified keyword, all you have to do is type that keyword in the text box labeled Search in Results and then press Enter. The moment you do that the grid will filter out any items that do not include that keyword in at least one of the columns. For example, here’s what we get when we type in the keyword network and then press ENTER:

Nice, huh? But it gets even better. For example, we could target our data display even more finitely simply by specifying a particular column where the keyword must occur. Only interested in services where the Name property includes the keyword network? Then type the desired column followed by a colon and a keyword, like so:

name:network

Notice that, this time around, we only get back information for a single service. Why? Because we only have one service that has the string network somewhere in its Name:

To clear the filtering, just delete the information in the Search box and press ENTER.

Saving Data

Before you ask, the answer is no: you can’t save data from the grid itself. (That would be cool, but there’s no way to do that.) But here’s a consolation prize. Suppose you run this command, which retrieves service data and then saves that data as an XML file named C:\Scripts\Test.xml:

Get-Service | Export-CliXML C:\Scripts\Test.xml

Big deal, you say? You wanted the best of both worlds: data displayed in a gird and data that could be saved for later use. Instead, all you got was the data saved as an XML file; you didn’t get a grid at all.

Well, maybe not directly. But try running this command:

Import-CliXML C:\Scripts\Test.xml | Out-GridView

So what will you get when you run that command? Well, we hate to spoil the surprise. But give it a try; for once, at least, your surprise should be a very pleasant one.