The final Out cmdlet we're going to explore is a
pretty cool one named Out‑GridView.
Let's dive back into the console one last time.
Hopefully, by now you've guessed what we're going to take a look at first.
Yep, the help file.
Let's check out the help for Out‑GridView.
So, OutGridView will send the output from a command to a grid view
in an interactive table in a separate window.
Because this cmdlet requires actual user interface,
it's only available in Windows systems that support the Windows desktop,
but I've got a bonus command to show you towards the end of
this clip as well that works cross‑platform.
Let's check it out, because this one is pretty cool.
We'll run Get‑Process again and pipe that across to
Out‑GridView and give that a go.
After a moment,
we get this external pop‑out window displayed that has the
output from the Get‑Process command.
Let me make that full screen,
and we'll have a look at a couple of the features that
are here in this user interface.
So firstly, I can sort any of these columns here just by clicking on them,
so I could sort the Id and I could also sort then on the ProcessName.
At the very top of the window, there's a filter box,
which can be used to filter on any of the content that's in the output here.
So in there I will type logon,
and the window filters to the only result that contains the word logon.
Underneath the filter window,
there's an option to be more specific and add criteria to filter on,
so I'll select the Id and the ProcessName properties. And for the
ProcessName let's filter on those that contain svc, and for the Id
we can click on this condition and we get a number of standard
conditions to select from.
For this one, I'll choose the is between condition,
and let's look for any that have an Id between,
say, 4,000 and 6,000.
Okay, there's only one result there,
so let me change that to between 2,000 and 6,000.
And the results update in real time, and we've now got four results there.
You can rearrange these columns in the view.
So if I click on the ProcessName heading here and then drag it across,
it lets me move the location so I could then reshuffle some of
these columns around to get what I'm looking for.
Now let's go back to the console and try a few other things.
You can use select object to select just a few properties and pipe those down
to Out‑GridView and only those properties will be displayed.
Alternatively, you could select all properties and pipe those to Out‑GridView,
and indeed, all of them will be displayed as well.
It does take a little bit longer here to render because of
the size of the data coming through, but down the bottom here,
I've now got this horizontal scroll bar with all of these
properties per object being displayed.
If I rerun the command to select just the name and the
id and send those to Out‑GridView,
but this time I want to use a couple of different parameters of Out‑GridView.
The first one is Title,
which allows you to set a custom name for the title of the
window that pops up, so I'll just call it MyData.
The second parameter is named PassThru, which tells the
cmdlet to send items down the pipeline from the interactive
window as input to other commands.
So this one here is probably another exception of where an Out cmdlet can
actually be manipulated to send data down the pipeline,
which it normally wouldn't do by default.
So if I were now to pipe that to Export‑Csv and pick a
path where I want to save that CSV, I'll show you how the PassThru works.
There's our Out‑GridView, and notice at the top that it's got a title of MyData.
But notice in the background that in the PowerShell console we
haven't been given our command prompt back.
That's because we used the PassThru parameter,
and this command is now waiting for data to send
down the pipeline to Export‑Csv.
Also, down the bottom right there, there are now these OK and
Cancel boxes which weren't there before.
I'm going to hold down the Ctrl button and then click on a
few different processes in the output here,
and then I'll click on the OK button and we get returned to the PowerShell
console and we've got the prompt back as well. Let me run import‑csv and specify
the path of the CSV file from that previous command,
and it's pulled in the data that I selected from the grid window.
So when I selected that data,
the Pass‑Thru parameter told Out‑GridView to send that data,
and only that data, down the pipeline,
which was down to Export‑Csv. Last thing for Out‑GridView,
and I have mentioned this elsewhere in the course,
but I want to cover it again now while we're here, is that this is the
only Out cmdlet that cannot accept formatted data.
If we grab the processes again and pipe that to Format‑List and then try
and send formatted data to Out‑GridView, we'll get an error that the data
format is not supported by Out‑GridView.
Now that's everything that I've got for you for Out‑GridView. but I do
want to show you another cool command that is similar,
and it's cross‑platform.
It's a bit of a bonus, really.
To get this command,
we need to install a PowerShell module from Microsoft named ConsoleGuiTools,
which we can do by running Install‑Module
Microsoft.PowerShell.ConsoleGuiTools. Depending on whether you have
installed anything from the PowerShell Gallery before or not,
you may or may not get this prompt.
I do trust the PowerShell Gallery,
so I'm going to type y there to proceed. And after a few moments,
the module is now installed. Let's run Get‑Command ‑Module
Microsoft.PowerShell.ConsoleGuiTools to see what commands are
available in the module that we've just installed.
And there's only one, which is Out‑ConsoleGridView.
We'll give that a try by running Get‑Process and
piping it to Out‑ConsoleGridView.
Whoa, how cool is that?
It's a similar grid view to what we just worked with,
but this time it's running from inside of the console session
rather than being an external GUI tool.
I can press my down arrow here to move down the objects, and then
I can press the space bar to mark the items.
So let me mark a few of these, and you can see them getting marked with an x.
If I press Enter to accept, it takes me back to the PowerShell console,
and it has output the objects that I selected while I was in the grid view.
Let's run it again to go back in there and have another look
around. At the top here, we've got a filter box,
so I could type something like srv, which will filter the results on the fly.
I could also enter something like 22, which will filter the
objects with 22 in any of the property values.
I'm going to escape out of that and go back to the console and we'll run
the same command again, and it's actually got a parameter named filter
where we can pass an object to filter through.
If I type svc here, notice the output is immediately filtered with
that value. Similar to Out‑GridView, there's also a Title parameter,
so I'll set that to MyGridView, and at the top left here it's
changed the title of the output.
Lastly,
there is an OutputMode parameter which allows you to change it
from the default to something like Single.
So if I were to do that, this time if I use the arrows to scroll
through the objects and select one by pressing space, this time it
puts a circle there instead of an x.
If I select another object by using the space bar, it moves the circle as
I'm now only allowed to select a single object at a time.
If I press Enter to accept that, it will return the output of the single result back on screen.