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

Checking how long this takes to execute with Measure-Command:

TotalMilliseconds : 317.5727

Pretty fast!

Now, let’s take a look at filtering with a Where-Object

Get-WmiObjectwin32_volume-ComputerNameServer1 | Where-Object {($_.drivetype -eq 3) -and!($_.driveletter)}

Measure-Command for it:

TotalMilliseconds : 366.9919

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:

Format-Table Name, @{ Label =“FreeSpace (GB)”; Expression={($_.freespace / 1GB)

.tostring(“F0”)}}

I use an expression to calculate a value for the “FreeSpace (GB)” column. For more information on expressions, see the article Windows PowerShell Tip of the Week: Creating Custom Tables on TechNet. I also use the tostring() method to format the free space. There are many sites that discuss formatting strings. I found the following two articles: Using Format Control Strings on the windows PowerShell Blog and Standard Numeric Format String at MSDN.

So, as a one liner, with some sample output:

Get-Wmiobject -query“select name,driveletter,freespace from win32_volume where drivetype=3 AND driveletter=NULL” -computerServer1 | Format-Table Name, @{ Label =“FreeSpace (GB)”; Expression={($_.freespace / 1GB).tostring(“F0”)}}

Name       FreeSpace (GB)
—-       ————–
L:\Log01\  30
L:\Log02\  23
L:\Log03\  20

I love when a task can be completed in one line.

~Daniel

Advertisements

7 thoughts on “PowerShell: Using WMI to query NTFS Mount Points

  1. Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian, iwspo.net

  2. When I run your first and last examples I get the following error:

    Get-WmiObject : Invalid query
    At line:1 char:14
    + get-wmiobject <<<< -query "select name,driveletter,freespace from win32_volume where drivetype='3' AND driveletter=N
    ULL" -computer bheidemann-d1
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    However Get-WmiObject win32_volume -ComputerName Server1 | Where-Object {($_.drivetype -eq 3) -and !($_.driveletter)} works fine

  3. When I run your first and last examples I get the following error:

    Get-WmiObject : Invalid query
    At line:1 char:14
    + get-wmiobject <<<< -query "select name,driveletter,freespace from win32_volume where drivetype='3' AND driveletter=N
    ULL" -computer bheidemann-d1
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    However Get-WmiObject win32_volume -ComputerName Server1 | Where-Object {($_.drivetype -eq 3) -and !($_.driveletter)} works fine

    1. How embarrassing! Thanks for catching the error. I clearly did something wrong while trying to “pretty up” the code for the blog.

      The problem lies in the single quotes around the 3. So, the following line should execute properly:

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

      -Daniel

  4. Did a little modification, just to get a tad more info:

    Get-Wmiobject -query “select name,driveletter,capacity,freespace from win32_volume where drivetype=3″ | Format_Table Name, @{ label=”SizeGB”; Expression={($_.capacity / 1GB).tostring(“F0″)}}, @{label=”FreeGB”; Expression={($_.freespace / 1GB).tostring(“F0″)}}, @{label=”FreePct”; Expression={($_.freespace*100/$_.capacity).tostring(“F0”)}}

    But great example which enabled me to made just what I needed in a jiffy :-)

  5. 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 :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s