Drive Data in GB and Percent

by Mar 14, 2014

When a cmdlet returns raw data, you may want to convert the data into a better format. For example, WMI can report the free space of a drive but reports bytes.

You can then use Select-Object and provide hash tables to take the raw data and convert it to anything you want. This sample illustrates how to convert Freespace into GB and also calculate the percentage of free space:

$Freespace = 
@{
  Expression = {[int]($_.Freespace/1GB)}
  Name = 'Free Space (GB)'
}

$PercentFree = 
@{
  Expression = {[int]($_.Freespace*100/$_.Size)}
  Name = 'Free (%)'
}

Get-WmiObject -Class Win32_LogicalDisk | 
  Select-Object -Property DeviceID, VolumeName, $Freespace, $PercentFree 

Here is the result without hash tables:

And this is the result using hash tables:

Twitter This Tip! ReTweet this Tip!