Menu

PowerShell: Using WMI to query NTFS Mount Points

I was recently approached to find out if it is possible to get the amount of free space available for all mount points on a system. Reasons for using mount points are many, but the most common is the desire to have more than 20 some-odd volumes. Using drive letters is obviously limited by the number of letters in the alphabet. But, how does one monitor how much space is left on mount point volumes?

As is frequently the case, WMI has the necessary information. The Win32_volume class has exactly what we need. From the MSDN article on the class (http://msdn.microsoft.com/en-us/library/aa394515(VS.85).aspx): The Win32_Volume class represents an area of storage on a hard disk. The class returns local volumes that are formatted, unformatted, mounted, or offline.

A quick look at the properties shows that it has a Drivetype property to help us limit the results to local disks. There is also a property for drive letter. We can use this to filter out the volumes that are not mount points. And, for this example, there is also a freespace property. With this, we should have everything we need.

I can quickly think of two ways to filter the data returned by the WMI class. We can either filter at the time of query with the get-wmiobject cmdlet, or we can use a where-object statement to filter.

Using a Get-WMIObject filter:

get-wmiobject-query“select name,driveletter,freespace from win32_volume where drivetype=3 AND driveletter=NULL”-computerServer1

Also pretty fast! given how close the two are, I’d venture to say the execution time is nearly identical. If performance is potentially an issue, it may be worth testing several times against multiple servers.

Finally, the output from both of these is a little messy. Let’s use a Format-Table to clean it up and return the free space in gigabytes. This is easily done by piping to:

Also, be careful when copying/pasting from the forum; it garbles the quotes (just delete all quotes and replace them yourself; the forum automatically replaces quotes with either opening or closing quotes :)