Ezine 201 – WMI Classes to Examine Disks

Ezine 201 – WMI Classes to Examine Disks

This week we are going to combine PowerShell and WMI and examine disk properties.  Once we master the basics of get-Help and get-Member, then we are free to explore all the WMI disk objects.

Topics for WMI Classes to Examine Disks


This Week’s Secret

Three good things happen whenever I rummage through WMI’s classes.  Invariably, I discover something about my machine; in the case of Win32_LogicalDisk I found a forgotten partition.  Working with WMI objects always teaches me something new about pure PowerShell commands, for example, Math truncate.  Another source of pleasure is that research one WMI leads to interesting sister classes such as Win32_DiskQuota or Win32_DiskDrive.

This Week’s Mission – Research WMI Disk Classes

My mission this week is to introduce you to a subset of WMI’s classes which features disk properties.  While I focus on just one class, Win32_LogicalDisk, please just regard this as an example.  It is my hope that you will discover more disk objects, and thus solve a particular problem on your computer.

Get-Help Get-WmiObject -full

All top athletes practice basic routines until they become second nature.  When top PowerShell scripters start using a cmdlet, such as get-WmiObject, they invariably run get-Help and get-Member.

get-Help get-WmiObject -full

Let us research WmiObject classes containing disk.

get-WmiObject -list | Where {$_.name -match ‘disk’ -and `
$_.name -match ‘Win32’ }  | ft name, properties -auto

Note 1:  Please note the backtick which enables the command to word-wrap onto the second line, in particular, there should be no space after the backtick (`)

Now to list the Properties of LogicalDisk

Get-WMIObject Win32_LogicalDisk |get-Member `
-memberType property [a-z]*

Note 2:  The purpose of  the -memberType line is to filter just for properties, and to excluding those beginning with __.

Basic Disk Reporting

Get-WmiObject Win32_LogicalDisk | format-Table Name, Size

Fancy Disk Reporting – Same Output, But with Better Formatting.

Get-WmiObject Win32_LogicalDisk | ft Name, @{Label = "Disk GB"; `
Expression={[math]::truncate($_.Size / 1GB)}} -auto

Note 3:  Ft is an alias for format-Table.  Incidentally, gm is an alias for get-Member.

How to Measure Freespace

Let us analyze how we appened the additional command, which adds a free diskspace column to the results.

@{Label = "Free GB";Expression={[math]::truncate($_.freespace / 1GB)}}

@{Label tells PowerShell we want a nice heading, otherwise we get a confusing literal:
{[math]::truncate($_.freespace / 1GB)}

The business end of this command is split into two parts, [math]::truncate, and divide by 1 gigabyte.  Sandwiched in between is $_.freespace, where freespace is a property of Win32_LogicalDisk.

Get-WmiObject Win32_LogicalDisk |ft Name, @{Label = "Disk GB"; `
Expression={[math]::truncate($_.Size / 1GB)}}, @{Label = "Free GB"; `
Expression={[math]::truncate($_.freespace / 1GB)}} -auto

WMI Monitor DetailsSolarwinds WMI Software

Here is another chance to download this free WMI Monitor.  It enables to you to measure the real-time performance of any modern Microsoft operating system.  Your FREE WMI Monitor will deliver a desktop dashboard that monitors any Windows server and offers built-in templates plus community customizable templates.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, Exchange mailboxes or SharePoint.

WMI Monitor Highlights:

  • Monitor real-time performance metrics on any Windows computer or application.
  • Leverage a large selection of pre-built and community generated application templates.
  • Modify or design your own application templates with the built-in WMI browser.
  • Use the WMI Monitor to familiarise yourself with classes that can be then used for PowerShell scripting.
  • Download Your Free WMI Monitor

Where Next?

Return to the basic techniques, this time seek another WMI class that looks interesting try:
get-WmiObject -list | Where{$_.name -match ‘disk’}

Next research properties for your chosen WMI class with:
get-WmiObject -class xyz | get-Member.


Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

If you like this page then please share it with your friends


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • Windows PowerShell   • PowerShell 3.0 Network

Win32_pingstatus   • WMI Win32_NetworkAdapter   • Win32_NetworkAdapterConfig

Disable NIC   • PowerShell -Filter  • PowerShell -Query   • PowerShell Select   • Free WMI Monitor

Please email me if you have any example scripts. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.