Windows PowerShell


PowerShell - Disk Check with Win32_LogicalDisk

PowerShell - Disk Check with Win32_LogicalDisk

Our mission is to investigate and interrogate a computer's logical disk.  Goals include discovering the size of each volume, and how much free space is available.  We also have pure PowerShell goals, for example, to examine the 'Select' statement, and to control the display with -groupby, |sort and -auto.

Topics for PowerShell Disk Check

 ♣

Trusty Twosome (Get-Help and Get-Member)

When you discover a new PowerShell command, it benefits from being surveyed with what I call the 'Trusty Twosome'.  I guarantee that if you research with Get-Help and Get-Member then you will reveal new scripting possibilities for get-WmiObject Win32_LogicalDisk. To see what I mean try these two commands:

1) get-Help get-WmiObject
    (help gwmi) If you like abbreviations.
    (help gwmi -full) If you prefer examples.

Get-help unearths useful parameters such as -class and -computerName (-computer also works).

2) get-WmiObject Win32_LogicalDisk |get-Member -MemberType property
    (gwmi Win32_LogicalDisk | gm) If you enjoy aliases.
    (gwmi Win32_LogicalDisk| gm -Membertype property) If you are fond of filters.

Get-Member reveals a whole host of properties that you don't normally see in Explorer.

Example 1a - Display Logical Disk Information

Let us start with a simple script to display disk information.  We begin by using just the -query parameter combined with an SQL-like select statement.

# PowerShell cmdlet to display Logical Disk information
get-wmiobject -query "Select * from win32_logicaldisk" |Ft

Note  Out-GridView: PowerShell v 2.0 introduces a new cmdlet to control data display.  See more on how to pipe the results into out-GridView.

Example 1b - Display Disk Size and FreeSpace

In the example below, I introduce a variable called $Item to control the output.  If you would like to refine the output to your specification, then research with get-Member.  The parameter -auto helps to 'tighten' the data display.

# PowerShell cmdlet to display a disk's free space
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command split over two lines by a backtick `
get-wmiobject -query "Select * from win32_logicaldisk" `
|Format-Table $item -auto

Example 1c - PowerShell Innovations

  i) The example provides structure to the data thanks to the -groupby command in the last line.

ii) However, this example's neatest technical achievement is:
Select $([string]::Join(',', $Item)).  This means we can use one variable, $Item, to control both the properties for the select statement, and the output of format-Table.

iii) It also introduces the idea of interrogating other machines on the network, for this it uses the -computerName, incidentally, -computer works just as well.  Naturally, the first thing to alter in the script below is:
-computer YourMachine.

# PowerShell cmdlet to display a disk's free space
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command, which is split over three lines by the backtick `
get-wmiobject -computer YourMachine -query `
"Select $([string]::Join(',', $Item)) from win32_logicaldisk" `
|sort MediaType, DeviceID | Format-Table $item -auto -groupby MediaType

Example 1d - Where command added

The new feature of the example below is the 'Where MediaType' filter.  By now I hope that you have started adjusting the script according to your needs.

$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command split over four lines by a backtick `
get-wmiobject -computer YourMachine -query `
"Select $([string]::Join(',',$Item)) from win32_logicaldisk `
where MediaType=12" |sort MediaType, DeviceID | `
Format-Table $item -auto

  ˚

Example 2 Win32_DiskPartition

The first purpose of this script is to remind you that WMI can access at least 500 win32 objects, of which 20 contain the word 'Disk'.  The second purpose is to show what information is revealed by a specific object namely: win32_DiskPartion.

# PowerShell cmdlet to display a disk's partition information.
$Item = @("Name","DiskIndex", "StartingOffset", "Bootable", "BlockSize", "NumberOfBlocks")
get-wmiobject -query "Select * from win32_DiskPartition" |Format-Table $item -auto

Learning Points

Note 1:  Remember get-Member.  Here is a classic situation to employ get-Member to discover what other properties are available for fin-tuning $Item.  This is the command to explore:

get-wmiObject win32_DiskPartition | get-member.

Note 2:  The basis of this script was kindly supplied by Jimmy May.  Please send me your ideas for scripts.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Summary of PowerShell with Win32_LogicalDisk

The central feature of this page is the WMI command get-WmiObject Win32_LogicalDisk.  From that base we modify the script to select properties such as, FreeSpace and disk size.  Along the journey we sharpen our skills to filter with 'Where', and also to collate the data with -groupby.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.