PowerShell Ezine, Logon Scripts

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.

 

If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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

 


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • PowerShell -Query   • PowerShell Select

Win32_pingstatus   • WMI Win32_NetworkAdapter   • Win32_NetworkAdapterConfig

Disable NIC   • PowerShell -Filter   • Windows PowerShell   • PowerShell 3.0 Network

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information.  Take the guess work out of which WMI counters to use for scripts.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.