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.
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.
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:
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.
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.
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.