WMI Scripts

WMI - Logical Disk Properties

Introduction to WMI Disks - Logical Disk Properties

When you investigate Disk activity, whether with WMI scripts or Performance Monitor, you should research more than one disk object.  The classical divide is between 'Logical' or 'Physical' disk.  In the case of WMI we have the following two Win32 objects:

Win32_LogicalDisk - Partition size, Drive Type and FreeSpace. (This page)

Win32_DiskDrive - Physical Disk data. Based on the number of blocks, tracks or sectors. See Here

Once you have mastered the basics of WMI, and Win32_LogicalDrive, you may wish to investigate extra properties such as DriveType or MediaType.  What happens with scripting is that when you discover new properties, your mind races ahead and thinks of ways to exploit the new knowledge.  For example, the property DiskType could give you the idea of taking action depending on whether the partition is, Fixed Hard Disk, CD, or 'Removal Drive'.  To give a specific example, if the drive is Removable then you probably don't want to install software or save a profile on that partition.

Topics for WMI - Disks

 ♣

Example 1 - VBScript to Display Logical Disk Information

Let us suppose you need to know how many partitions there on a Windows 2003 Server, moreover, you wish to record the size and free space of each partition.  In fact I have selected about a dozen LogicalDisk properties and rejected a further 20.  My criteria were, does the property usually return a result and is this a mainstream feature of a disk.

Prerequisites for your Logical Disk Information WMI Script

None.  Just take care with the _ Underscores which indicate the instruction wraps to the next line.

Instructions for your Logical Disk Information WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide which machine on your network to interrogate and then change line 10:
    strComputer = "NetworkMachine" to the name of that machine.  Alternatively change strComputer to the local machine by substituting a period or "."  strComputer = "."
  3. Save the file with a .vbs extension, for example: LogicalDisk.vbs 
  4. Double click LogicalDisk.vbs and check the properties of each partition.

Script to Interrogate Logical Disk

' LogicalDisk.vbs
' Sample VBScript to interrogate a Logical disk with WMI
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - November 2010
' -----------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer

On Error Resume Next
strComputer = "."

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")

For Each objItem in colItems
Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
" ==================================" & VbCr & _
"Drive Letter: " & objItem.Name & vbCr & _
"Description: " & objItem.Description & vbCr & _
"Volume Name: " & objItem.VolumeName & vbCr & _
"Drive Type: " & objItem.DriveType & vbCr & _
"Media Type: " & objItem.MediaType & vbCr & _
"VolumeSerialNumber: " & objItem.VolumeSerialNumber & vbCr & _
"Size: " & Int(objItem.Size /1073741824) & " GB" & vbCr & _
"Free Space: " & Int(objItem.FreeSpace /1073741824) & _
" GB" & vbCr & _
"Quotas Disabled: " & objItem.QuotasDisabled & vbCr & _
"Supports DiskQuotas: " & objItem.SupportsDiskQuotas & vbCr & _
"Supports FileBasedCompression: " & _
objItem.SupportsFileBasedCompression & vbCr & _
"Compressed: " & objItem.Compressed & vbCr & _
""
Next

WSCript.Quit

' End of Sample DiskDrive VBScript

WMI Tutorial Learning Points

From a WMI perspective

1) GetObject("") is a well-known VBScript method for fetching an instance of an object.  Winmgmts is the shell, which looks after the CIM schema and objects.  In other examples, in place of winmgmts, you may find wscript, LDAP:// or WinNT://.  If it would aid your understanding, you could type winmgmt /? at the command prompt.

2)  You will soon appreciate that the WMI section of the script begins by calling winmgmts.  Specifically, winmgmts gives access the root of the CIM library, here is the command:
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")

3) Set colProcess = objWMIService.ExecQuery _ is a standard WMI phrase, which prepares for the WQL command:  Select * from Win32_LogicalDisk
The part of this command that we are particularly interested in is _LogicalDisk.  WMI has 4 different types of Win32 Disk object we need to query the LogicalDisk component and not the DiskDrive.

From a VBScript perspective

4) strComputer is a classic example of naming a VBScript variable.  It is good practice to name variables after the object to which they refer, in this case the computer.  The ".", dot or period means the current machine where the script is running.  Therefore, it would be easy to change the "." for the name of a server on your network.  The beauty of using variables is that you only need to set the value once, instead of hunting through the script for every reference to the computer that you wish to interrogate.

4) What makes scripting so powerful is the speed with which VBScript loops through an array of objects or properties, in this instance the loop is controlled by: For Each....In... Next.

5) It is also possible to output the process information not to the screen but to a text file.  On later pages, we will investigate how WMI calls VBScript to create a file and write the output to file.  However, for the present we will stick with Windows Script Host message boxes.

Note: If it's physical disk information that you're interested in then there is a different Win32 object called DiskDrive, which reports on properties such as SCSI Bus, track, sector or absolute disk size.  See here how to interrogate the Physical Disk.

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

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 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 - Using Select Case to Display Drive Type

Many WMI tasks require specific information, for example before you install software you need to know the amount of free space and possibly the drive type.  This script interrogates the operating system to see whether the drive type is removable, local hard disk or some other type.

' DriveType2.vbs
' Sample VBScript to discover the drive type with WMI
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.6 - November 2010
' -----------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer
Dim strDriveType, strDiskSize
On Error Resume Next
strComputer = "."

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")

For Each objItem in colItems
Select Case objItem.DriveType
Case 1 strDriveType = "Drive could not be determined."
Case 2 strDriveType = "Removable Drive"
Case 3 strDriveType = "Local hard disk."
Case 4 strDriveType = "Network disk."
Case 5 strDriveType = "Compact disk (CD)"
Case 6 strDriveType = "RAM disk."
Case Else strDriveType = "Drive type Problem."
End Select

If objItem.DriveType =2 Then
strDiskSize = Int(objItem.Size /1048576) & " Mega Bytes"
Else
strDiskSize = Int(objItem.Size /1073741824) & " GB"
End If

Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
" ==================================" & VbCr & _
"Drive Letter: " & objItem.Name & vbCr & _
"Drive Type : " & strDriveType & " " & strDiskSize & vbCr & _
"Free Space: " & Int(objItem.FreeSpace /1073741824) & _
" GB" & vbCr & _
""
Next

WSCript.Quit

' End of Sample DiskDrive VBScript

WMI Tutorial Learning Points

From a WMI perspective

1) This script focuses on the Win32_LogicalDisk, DriveType property.  However, it also reports on the size of the partitions.

From a VBScript perspective

2)  I love the Select Case statement.  Wherever there are multiple outcomes I see if Select Case would be less cumbersome than multiple If then.. Else statements. 

DriveType can return any number from 1 to 6.  Moreover, as a number, the output is less useful than a descriptive string value.  In particular, we would wish to know if the partition is "Removable Drive", "Compact disk"  or "Local hard disk".  Dealing with a number means you have to memorise whether DriveType 2 = Removable Drive or Local had drive.  Wearing your VBScript hat, displaying, Drive Type : Local hard drive, costs little and gives a more meaningful output than, Drive Type : 3.

3) When it comes to outputting the disk size, removable disks tend to be in the mega range whereas fixed disks are more likely to be in gigabytes. 

In case you thought I did not like the If construction, here I happily employ, If .. Then .. Else... End If statement.  Note how the size of the removable disk is divided by 1048576 (1024 x 1024), and that the fixed disk is divided by a number 1024 times bigger (1024 x 1024, 1024).  For me it's a matter of horses for courses, sometimes Select Case wins, sometimes If...Then, Else .. End If is more effective.

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds' Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you've already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its 'Traps' and 'Communities'. Try Config Generator now - it's free!

Download your free copy of Config Generator

Example 3 - Using Select Case to Display Drive Type - Cosmetic Improvement

Just a cosmetic change on the previous example to display all the disk drives in one message box.

' DriveType3.vbs
' Sample VBScript to discover the drive type with WMI
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.1 - July 2006 Correction by Olav Meijer
' -----------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer
Dim strDriveType, strDiskSize, strDisk
On Error Resume Next
strComputer = "."

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")

For Each objItem in colItems
Select Case objItem.DriveType
Case 1 strDriveType = "Drive could not be determined."
Case 2 strDriveType = "Removable Drive"
Case 3 strDriveType = "Local hard disk."
Case 4 strDriveType = "Network disk."
Case 5 strDriveType = "Compact disk (CD)"
Case 6 strDriveType = "RAM disk."
Case Else strDriveType = "Drive type Problem."
End Select

If objItem.DriveType =2 Then
strDiskSize = Int(objItem.Size /1048576) & " Mega Bytes"
Else
strDiskSize = Int(objItem.Size /1073741824) & " GB"
End If

strDisk = strDisk & vbCr & _
"Drive Letter: " & objItem.Name & vbCr & _
"Drive Type : " & strDriveType & vbCr & _
"Disk Size : " & strDiskSize & vbCr & "Free Space : " _
& Int(objItem.FreeSpace /1073741824) & " GB" & vbCr & _
" ========================="

Next
Wscript.Echo strDisk

WSCript.Quit

' End of Sample DiskDrive VBScript

Learning Points

Now that you are growing in experience, this maybe a suitable time just to 'play' with the script.  You could start with cosmetic changes to the separators.  Replacing === with ####.  Then progress to changing " Mega Bytes" to " Mb".  My point is that making even simple changes you will miraculously gain in confidence and soon you will be making more widespread changes.

Summary of Logical Disks, DiskDrive and WMI

The Win32_LogicalDrive object exposes properties such as the type of partition and how much free space is on the disk.  As with most of the WMI scripts, it effortlessly cycles through the properties and displays the values for each partition or disk drive in a message box.

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

 


See more VBScript WMI examples:

WMI Tutorial   • Win32_Process   • WMI Memory   • WMI Basics   • Free Download of WMI Monitor

WMI VBS   • VBScript Services   • WMI Disks   • WMI Physical Disks

WMI Home   • WMI Win32   • WMI Printer   • VBScript Echo   • WMI VBScript

 *


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-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: