WMI is really one of the coolest things ever but sometimes the raw output could use a some filtering and formatting help for better readability. For example, Win32_LogicalDisk by itself will pull information on all your drives including mapped network drives, DVD, RAM Disks, and even virtual drives but most of us are really only interested in the our local hard drive. Also, the size and free space are represented in bytes which makes for some huge numbers given today’s typical hard drive.
Side note, “gwmi” is the alias for “Get-WmiObject” and I’ll be using both forms in this post.
Example Win32_LogicalDisk output:
PS C:\powershell> gwmi Win32_LogicalDisk DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 33193398272 Size : 95709319168 VolumeName : DeviceID : F: DriveType : 5 ProviderName : FreeSpace : Size : VolumeName : DeviceID : G: DriveType : 5 ProviderName : FreeSpace : Size : VolumeName :
We can make this more readable by using Format-Table with the -autosize option (-autosize will compact the columns of data to the left margin):
PS C:\powershell> gwmi Win32_LogicalDisk |format-table -autosize DeviceID DriveType ProviderName FreeSpace Size VolumeName -------- --------- ------------ --------- ---- ---------- C: 3 33193394176 95709319168 F: 5 G: 5
That is much better now, but I am not interested in my F: and G: drives – local hard disk is what I care about and I’d also like to see the FreeSpace and Size in a more human-readable format. This is all do-able with a little fine tuning but there’s a catch: it’s a bit more code than you will want to type interactively at the command-line so your going to probably want to use a small script.
This is a very simple script I’ve called Get-LocalDisk.ps1
In a nutshell, here’s what it does:
- Accepts one argument for a host name to query (use “localhost” if running on your current computer)
- Uses the SELECT query to only retrieve local drives (DriveType=3) and disregard mapped drives, CD-Roms, etc.
- Modifies the Heading Labels for FreeSpace and Size to indicate unit of measurement is now in Gigabytes
- Uses the “Alignment” specifier to keep the numbers right-justified
- Uses the “Expression” specifier to provide a numerical format with two decimal precision
- Divide the raw WMI byte data by 1 Gigabyte (1GB) to convert to Gigabytes
- Lastly, pipe the formatted results through “out-default”; - this is not necessary for a single WMI/Format-Table operation but you may recall that if several calls like this are stacked up in a script, PowerShell seems to have some issues streaming the output and will sometimes produce errors. To be on the safe side, I’m just getting into the habit of piping Format-Table this way.
- One final note, if copying this script be sure to preserve the back-tick “`” characters for line-continuations
# Get-LocalDisk.ps1 $hostname = $args[0] Get-WmiObject ` -computer $hostname ` -query "SELECT * from Win32_LogicalDisk WHERE DriveType=3" ` | format-table -autosize ` DeviceId, ` VolumeName, ` @{Label="FreeSpace(GB)"; ` Alignment="right"; ` Expression={"{0:N2}" ` -f ($_.FreeSpace/1GB)}},` @{Label="Size(GB)"; ` Alignment="right"; ` Expression={"{0:N2}" ` -f ($_.size/1GB)}} ` | out-default |
Finally, here is our nicely formatted output. System Administrators will find this much more interesting when querying multiple hosts across their network.
PS C:\powershell> ./get-localdisk localhost DeviceId VolumeName FreeSpace(GB) Size(GB) -------- ---------- ------------- -------- C: 30.91 89.14
Recent Comments