PowerShell Check Disk Space - Win32_LogicalDisk

PowerShell Scripts - Check Disk 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 Get-WmiObject Win32_LogicalDisk

 ♣

Trusty Twosome (Get-Help and Get-Member)

Each new PowerShell command that you discover will benefit from being interrogated by 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.  Let us start with Get-Help.

Preparation with PowerShell's Get-Help

Before we use PowerShell's Win32_LogicalDisk, let us investigate the master cmdlet Get-WmiObject.  In particular, we need to understand the syntax of parameters such as -Class and -ComputerName.  Incidentally, you can use the alias gwmi instead of Get-WmiObject.

# Help with PowerShell WMI object:
Get-Help Get-WmiObject

Note 1: If you prefer to see examples append -full, thus: help Get-WmiObject -full

How to Research Get-Wmi Object Disk Classes

# Check WmiObject Classes
Clear-Host
$Type = "Disk"
Get-WmiObject -List | Where-Object {$_.name -Match $Type}

Note 2: The above script is setup so that you can easily edit $Type, for example, "Win32_Disk".  See more on the $_ .name variable.

Example 1: To Display Logical Disk Information

# PowerShell cmdlet to display Logical Disk information
Clear-Host
Get-WmiObject Win32_logicaldisk | Format-Table -auto

Those with a VBScript background may be more familiar with query and select.  This achieves the same result by using just the -query parameter combined with an SQL-Like select statement.

# PowerShell cmdlet to display Logical Disk information
Clear-Host
Get-WmiObject -query "Select * from Win32_logicaldisk" |Ft

Research Properties with Get-Member

# Properties for PowerShell logical disk object:
Get-WmiObject Win32_logicaldisk | Get-Member

Note 3: If you enjoy aliases then try: gwmi Win32_logicaldisk| gm.

Note 4: If you are fond of filters: gwmi Win32_logicaldisk| gm -Membertype property.

Get-Member (gm) reveals a whole host of properties that you don't normally see in the Disk Manager.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Example 2 - Display Disk Size and FreeSpace

Following the above research, we now have extra information about the properties of Win32_logicaldisk, consequently we create a PowerShell script which collects disk space statistics.

# PowerShell command disk space
Get-WmiObject Win32_logicaldisk `
| Format-Table DeviceId, MediaType, Size, FreeSpace -auto

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 2a - Display Disk Size and FreeSpace

Warning!  Confession!  One day I was just playing and made this more script which achieves the same as the above, however it does prepare us for a more complicated script below.

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 2b - Controlling the Decimals

This script is a digression which introduces 'Expression' to calculate freespace.  It also employs the -f format to control the decimal places (N0) and the percentage (P0).

Clear-Host
Get-WmiObject Win32_logicaldisk -ComputerName LocalHost `
| Format-Table DeviceID, MediaType, `
@{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
@{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
@{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
-AutoSize

Note 5: These scripts use localhost, you could change, or else remove this parameter.  See also Get-PSDrive method.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

Example 3: PowerShell Innovations

 i) The following 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 Get-WmiObject disk space
Clear-Host
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command, which is split over three lines by the backtick `
Get-WmiObject -computer LocalHost -query `
"Select $([string]::Join(',', $Item)) from Win32_logicaldisk" `
| Sort MediaType, DeviceID | Format-Table $item -auto -groupby MediaType

Example 3b: 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.

# PowerShell check disk space
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
Clear-Host
# Next follows one command split over four lines by a backtick `
Get-WmiObject -computer LocalHost -query `
"Select $([string]::Join(',',$Item)) from Win32_logicaldisk `
Where MediaType=12" | Sort-Object MediaType, DeviceID `
| Format-Table $item -auto

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer's Toolset v10

This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it's like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer's Toolset now!

Download your fully functional trial copy of the Engineer's Toolset v10

Example 4: 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.
Clear-Host
$Item = @("Name","DiskIndex", "StartingOffset", "Bootable", "BlockSize", "NumberOfBlocks")
Get-WmiObject -query "Select * from Win32_DiskPartition" | Format-Table $item -auto

Learning Points

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

Get-WmiObject Win32_DiskPartition | Get-member.

Note 7: The basis of this script was kindly supplied by Jimmy May.  Please send me your ideas for scripts.  See more on disk problems.

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.  The result is a neat PowerShell script which collects disk space information.

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

 


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • Win32_ComputerSystem   • Free WMI Monitor

WMI Class  • [WMI] Type  • Win32_printer   • Win32_product   • SystemRestore   • WMI Services

WMI Disk   • DNS   • Memory  • PowerShell -Filter   • Check Server UpTime   • ConvertToDateTime

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

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: