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.
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.
very nice JH. Simple and very effective!