Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: A GridView Drive Report

Posted on November 2, 2012November 1, 2013

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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="Drive";Expression={$_.DeviceID}},
@{Name="SizeMB";Expression={[int]($_.Size/1MB)}},
@{Name="FreeMB";Expression={[int]($_.Freespace/1MB)}},
@{Name="UsedMB";Expression={[math]::round(($_.size - $_.Freespace)/1MB,2)}},
@{Name="Free%";Expression={[math]::round(($_.Freespace/$_.Size)*100,2)}},
@{Name="FreeGraph";Expression={
 [int]$per=($_.Freespace/$_.Size)*100
 "|" * $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 | out-gridview -Title 'WMI Detail: Please select one or more drives' -PassThru | 
 foreach {
  get-wmiobject -Class win32_logicaldisk -filter "deviceid='$($_.Drive)'" -computername $_.Computername | 
 Select * } | Out-GridView -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 | 
  out-gridview -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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun: A GridView Drive Report”

  1. coderaven says:
    November 2, 2012 at 11:57 pm

    very nice JH. Simple and very effective!

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d