PowerShell Pipeline

Viewing Your Drive Information with PowerShell

Using Windows Management Instrumentation makes it easy to pull information about the drives on your system or remote systems. Here's how.

Managing drives is a very common thing that a system administrator can do. Whether it is monitoring space to ensure that a drive doesn't run out of available capacity, to understanding just how many drives are on a single system, using PowerShell to provide information for this is something that every single system administrator who works with Windows should know!

Windows Management Instrumentation (WMI) can be a wealth of information that anyone should be able to query to locate pretty much whatever they need information on. In our search for information on all of the drives on our system, one of the more common WMI classes to query is the Win32_LogicalDisk class. This class contains information about all of the attached and mapped drives on a system. More information on this class can be found here.

We can run the following query using the Get-WMIObject cmdlet to find out more information about all of the drives on my system:

Get-WmiObject -Class Win32_logicaldisk

From just the currently formatted list view that was returned, we can see that we have the DeviceID, which is the drive letter assigned to the drive; the FreeSpace, which is measured in bytes, as well as the capacity (size) of the drive; along with the volume name that has been specified. One of the properties, DriveType, is useful because it states what kind of drive we are dealing with. It is always specified by a number, but we can look at the URL that I provided earlier to better understand what those numbers actually translate to, as shown in the table below:

Using this knowledge, we could easily filter our query for only drives that matched a certain type.

#Look at only local drives
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'"
#Find network mapped drives
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '4'"

While viewing the size in bytes is necessary to keep the size at the lowest possible level, sometimes it is nice to see it in a more readable format, such as in GB. Using custom properties with our object output, we can quickly find out what the free space and capacity of the drives are in GB.

Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | 
Select-Object -Property DeviceID, DriveType, VolumeName, 
@{L='FreeSpaceGB';E={"{0:N2}" -f ($_.FreeSpace /1GB)}},
@{L="Capacity";E={"{0:N2}" -f ($_.Size/1GB)}}

WMI is a vast repository and there are many, many different relationships between classes. It may seem a little challenging to find all of the relationships between these classes, but fortunately, there are a couple of methods that allow for easy spelunking to locate not only the relationships between classes, but also to pull up that class and view the appropriate object that is related to the source. In the case of Win32_LogicalDisk, we will explore the other classes which have relationships with this class, as well as dive into those classes to gather more useful information about the drive.

What we will do to limit ourselves to a single drive will be setting a filter for only the C:\ drive and then work with the object to find all of the relationships.

$Disk = Get-WmiObject -Class Win32_logicaldisk -Filter "DeviceID = 'C:'"

Now using the Win32_LogicalDisk object from WMI, we will take a look at all of the relationships to this class and then determine which ones interest us the most and take a deeper dive into that class.

$Disk.GetRelationships() | Select-Object -Property __RELPATH
[Click on image for larger view.]

It is important to note the __RELPATH property and the data after the CIMV2: (as highlighted) as this is the true class that is related to the Win32_LogicalClass, not what is in the __CLASS property. If we are interested in viewing all of the object relationships between the Win32_LogicalDisk class and the other three other classes, we can simply call GetRelated() without specifying anything as a parameter and all of the related classes will be shown.


That is great and all, but some of this is not really useful to us to gather more information about the drive. Instead, we can specify the particular class that has a relationship with my current class and provide the string name in the method.

$DiskPartition = $Disk.GetRelated('Win32_DiskPartition')

Here we can see information such as the number of blocks on the partition, whether it is a primary partition, and if it is the boot partition. We can also expand on the information by forcing all of the properties to display and look to find anything else that might be useful to us.

$DiskPartition | Select-Object -Property *

In this case, nothing really stands out to me that might be useful. So we can now move on and dive even deeper by looking for any unique relationships with this class. With luck, we can find something new to look at.

$DiskPartition.GetRelationships() | Select-Object -Property __RELPATH
$DiskDrive = $DiskPartition.getrelated('Win32_DiskDrive')

In this case, we found the Win32_DiskDrive class where, upon viewing the properties, we can see that there are some pretty interesting things.

$DiskDrive | Select-Object -Property *


[Click on image for larger view.]

On the initial view, we can see the model of the drive, as well as the number of partitions on the drive. Expanding the drive out via Select-Object –Property * shows more information such as its capabilities (shown as a numeric value with Capabilities, as well as its human-readable format with CapabilitiesDescription) and the firmware revision on the drive.

The last thing that I will cover here is the Win32_Volume class, which provides another look at our drives, as well as provides information about whether the item happens to be a mount point.

$Volume = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'C:'"
[Click on image for larger view.]

Similar to Win32_LogicalDisk, we can dive into the relationships of the class that includes Win32_QuotaSetting and Win32_MountPoint (none of which are enabled) to further get more information about how the volume is configured.

When working with WMI, you can see just how easy it is to pull information about the drives on your system or remote systems. You can take this knowledge about pulling drive information and apply this to your enterprise to better understand what drives are being used in your environment.

About the Author

Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.