How Can I Retrieve Disk Quota Information for a Single User?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I retrieve disk quota information for a single user?

— AB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AB. There are a couple different ways to get disk quota information for a single user, depending on which version of Windows you have installed on the computer. If you’re running Windows XP or Windows 2003, you can retrieve disk quota information for a user via the Win32_DiskQuota class. For example, here’s a script that retrieves disk quota information for user kenmyer:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colQuotas = objWMIService.ExecQuery _ (“Select * From Win32_DiskQuota Where ” & _ “User=’Win32_Account.Domain=””fabrikam””,Name=””kenmyer””‘”)

For Each objQuota in colQuotas Wscript.Echo “Disk Space Used: ” & objQuota.DiskSpaceUsed Wscript.Echo “Limit: ” & objQuota.Limit Wscript.Echo “Quota Volume: ” & objQuota.QuotaVolume

intStatus = objQuota.Status Select Case intStaus Case 0 strStatus = “OK” Case 1 strStatus = “Warning limit reached” Case 2 strStatus = “Quota exceeded” Case Else strStatus = “Unknown” End Select

Wscript.Echo “Status: ” & strStatus Wscript.Echo “Warning Limit: ” & objQuota.WarningLimit Next

OK, granted, the WQL query looks a little crazy (all those double quote marks!), but that’s just the way the Win32_DiskQuota class is designed: the User property is actually a reference property, which is a pointer to another object and another class (in this case, the Win32_Account class). Fortunately, while our might not pretty, it’s not all that hard to deal with. What if you want to get quota information for a user named carolphilips, whose account is in the contoso domain? That’s easy; just replace kenmyer with carolphilips and fabrikam with contoso:

Set colQuotas = objWMIService.ExecQuery _
    (“Select * From Win32_DiskQuota Where ” & _
        “User=’Win32_Account.Domain=””contoso””,Name=””carolphilips””‘”)

And at least the rest of the script is pretty straightforward; we just echo the values of the various properties. The only “fancy” thing we’ve added is a Select Case statement that converts the numeric value returned by the Status property to a readable string. For example, if Status comes back as a 2, the script converts that to the string Quota exceeded. Other than that, about the only thing you might want to do to this script is divide all the quota numbers by 1024. That would convert these values (which come back as kilobytes) to megabytes.

Incidentally, this script returns quota information for kenmyer on all the drives on the computer. What if you only want quota information for a single drive? Ah, you just had to ask that, didn’t you? This can be done; it’s just that the WQL query looks even weirder due to the fact that the QuotaVolume property is yet another reference property:

Set objQuota = objWMIService.Get _
    (“Win32_DiskQuota.QuotaVolume=’Win32_LogicalDisk.DeviceID=””C:””‘,” & _
        “User=’Win32_Account.Domain=””tomservo””,Name=””Administrators””‘”)

If you need to get information for a different drive, then just copy the preceding query and replace C: with the appropriate drive letter.

Of course, while this script works great on Windows XP and Windows 2003, it doesn’t do you much good on Windows 2000; that’s because the Win32_DiskQuota class isn’t supported on Windows 2000. (Nor is it supported on Windows NT 4.0 or Windows 98, at least in part because those versions of Windows don’t support disk quotas, period.) So what do you do if you need to get disk quota information for a Windows 2000 computer?

Well, in that case, you use this script, which takes advantage of the Shell object to get disk quota information for kenmyer for drive C: on the local computer:

Set colDiskQuotas = CreateObject(“Microsoft.DiskQuota.1”)
colDiskQuotas.Initialize “C:\”, True
Set objUser = colDiskQuotas.FindUser(“fabrikam\kenmyer”)

Wscript.Echo “Logon name: ” & objUser.LogonName Wscript.Echo “Quota limit: ” & objUser.QuotaLimit Wscript.Echo “Quota threshold: ” & objUser.QuotaThreshold Wscript.Echo “Quota used: ” & objUser.QuotaUsed

As you can see, this is actually a shorter, more elegant-looking script, and with relatively few double quote marks. So why don’t we recommend that you use this script on XP or Windows 2003? Well, you can if you want to. However, there are two limitations to retrieving disk quota information using the Shell object. First, Shell object scripts only run against the local computer; unlike a WMI script, you can’t connect to a remote machine and return disk quota information.

Second, you have to retrieve quota information one disk at a time. With WMI, you can return a collection that includes disk quota information for all the drives on a computer. With the Shell object, you’ll have to write code to get data from drive C:, then code to get data from drive D:, then code to get data from drive E:, etc.

0 comments

Discussion is closed.

Feedback usabilla icon