Tag Archives: Out-Gridview

Friday Fun A Graphical PowerShell History Picker

One of my favorite features in PowerShell 3.0 is that you can select items in Out-Gridview which will then pipe the object back to the pipeline. One way I’ve been using this is as graphical “picker” for command history. I use Get-History, actually its alias h, all the time. Once I know the history number I then use Invoke-History, or its alias r. Now, with Out-Gridview I can sneak in a little something extra.

PS C:\> h | ogv -p | r

I realize this is cryptic but when using the shell interactively efficiency is paramount. This is the actual full command.

PS C:\> Get-History | Out-Gridview -passthru | Invoke-History

I’ll get something like this:


I can select an item, click OK and the command will run back in my console. There are a few downsides, but remember this is a Friday Fun article. First, if you cancel, PowerShell will invoke the last command again. Also, as written if you select multiple items, which you could with -Passthru, you’ll get an error because Invoke-History won’t accept multiple entries. You could force Out-Gridview to only allow a single selection.

PS C:\> h | ogv -OutputMode Single | r

Or you could use Foreach to handle multiple selections.

PS C:\> h | ogv -p | %{r $_}

Be aware that when you run multiple commands in the same pipeline formatting can get a little screwy so I would probably stay way from it.

One more way you might use this is with Invoke-Expression instead of Invoke-History. The history object has a commandline property which you could invoke.

PS C:\> (h | select * | ogv -p).Commandline | iex

This will also handle multiple commands. If you cancel from Out-Gridview, Invoke-Expression will throw an exception but at least it won’t try to re-run the last command which is probably better.

Again, these are all “quick and dirty” commands to have fun with from a PowerShell prompt.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

PowerShell Graphing with Out-Gridview

I’ve received a lot of interest for my last few posts on graphing with the PowerShell console. But I decided I could add one more feature. Technically it might have made more sense to turn this into a separate function, but I decided to simply modify the last version of Out-ConsoleGraph. The new version adds a switch parameter called -Gridview. When used, the graphing information is sent to the Out-Gridview cmdlet. This version will require PowerShell 3.0 as I’ll explain.

I realized I could just as easily send the values for the object name, property and graphing figure to Out-Gridview. In the ForEach construct where each object is processed, if the -GridView parameter is specified, I’ll create an ordered hash table and add it to an array.

if ($GridView) {
    #add each object to the gridview data array
    $gvHash = [ordered]@{
    $CaptionProperty = $caption
    $Property = ($g*$graph)
    Value = $obj.$Property
    $gvData += New-Object -TypeName PSObject -Property $gvHash

I need an ordered hash table so that the property values remain in the same order. I could have piped the new object to Select-Object and specified the order, but hopefully many of you are moving to v3 anyway. There’s no need for me to pad the caption since the grid view lines things up in columns automatically. It is also easier to add a property with the actual property value. I wanted to incorporate that value into the console version, but it proved to be more complicated than expected. Using Out-Gridview is a nice alternative.

After all of the objects have been processed, if there is anything in $gvData, I send it Out-Gridview.

if ($gvData) {
      Write-Verbose "Sending data to Out-Gridview"
      $gvData | OutGridView -Title $Title

When using the grid, there is no way to specify color. But on the other hand, it is much easier to click on a column heading and re-sort. Here’s a command I ran with the new function.

Get-ChildItem C:\Scripts Directory | foreach {
  $data = Get-ChildItem $_.FullName -Recurse -File | Measure-Object -Property Length -sum
  $_ | Select Name,@{Name="Size";Expression={$data.sum}}
} | OutConsoleGraph -Property Size -Title "Scripts Folder Report" grid


Now I have a tool with some flexibility. Although you might also be interested in this article from last November on building a drive report graph also using Out-Gridview.

I bumped the latest version of Out-Consolegraph to 3.0 to align it with the file name. Download Out-ConsoleGraph-v3

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

Friday Fun: A GridView Drive Report

I’ve been experimenting with different techniques to work with PowerShell in graphical ways, but without resorting to complex solutions such as WinForms or ShowUI. For today’s Friday Fun I have a little script that presents a drive usage report using WMI and Out-GridView. As always, my goal with these articles is to impart a nugget of useful information, regardless of whether you need the complete solution.

Getting the drive data with WMI is pretty straightforward.

get-wmiobject win32_logicaldisk filter "drivetype=3"

But what I want is something I can send to Out-Gridview that will give me a graphical representation of drive utilization. So I’ll take this basic command and pipe it to Select-Object, and add a few custom properties.

$data = Get-WmiObject -class win32_logicaldisk -ComputerName $computername filter ‘drivetype=3′ |
Select @{Name="Computername";Expression={$_.Systemname}},
@{Name="UsedMB";Expression={[math]::round(($_.size $_.Freespace)/1MB,2)}},
 "|" * $per }

My custom properties reformat values into MB and a Free percentage. These values are formatted as numbers so they can be sorted. That much would be fine if you wanted to write that to the pipeline. But I’m going to get graphical so I also define a property called FreeGraph. The value is simply the “|” character displayed once for each percent of free space. Sure, I could write this to the pipeline and see something like this:

Computername : SERENITY
Drive        : G:
SizeMB       : 476938
FreeMB       : 102887
UsedMB       : 374050.49
Free%        : 21.57
FreeGraph    : !!||||||||||||||||||||||

But where this gets really interesting is where I pipe $data to Out-GridView. Here’s an example where I queried several computers.

Now I have a graphical report that I can filter and sort. You may have to manually resize the display and adjust columns but you get the idea. But as they say on late night TV, wait there’s more.

If you have PowerShell v3, Out-Gridview now supports passthru. You can select one or more objects in Out-GridView and they will be written to the pipeline. This leads to some interesting opportunities. Here’s a variation that opens the selected drives in a new gridview window, displaying all the WMI properties.

 $data | outgridview -Title ‘WMI Detail: Please select one or more drives’ -PassThru |
 foreach {
  get-wmiobject -Class win32_logicaldisk filter "deviceid=’$($_.Drive)’" -computername $_.Computername |
 Select * } | OutGridView -Title ‘WMI Drive Detail’

This gridview allows me to select multiple entries.

When I click OK, the objects are piped back to PowerShell and in this case back to Out-Gridview.

Or, maybe you simply need to open the drive on the remote machine. Since I’m querying logical disks, they should each have an administrative share which I can construct from the drive object, and then open using Invoke-Item.

  $data |
  outgridview -Title "Drive Explorer: Please select one or more drives to open" -PassThru |
  Foreach {
   #construct a UNC for the drive that should be the administrative share
   $unc = "\\{0}\{1}" -f $_.Computername,$_.Drive.replace(":","$")
   #open the UNC in Windows Explorer
   invoke-item $unc

In these examples Out-Gridview is set to allow multiple selections. If you prefer to limit selection to one object, then use -Outputmode Single in place of -Passthru.

I’ve put all of these commands in a script you can download and try for yourself.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati