{"id":70933,"date":"2004-11-24T15:39:00","date_gmt":"2004-11-24T15:39:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/24\/how-can-i-retrieve-disk-quota-information-for-a-single-user\/"},"modified":"2004-11-24T15:39:00","modified_gmt":"2004-11-24T15:39:00","slug":"how-can-i-retrieve-disk-quota-information-for-a-single-user","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-disk-quota-information-for-a-single-user\/","title":{"rendered":"How Can I Retrieve Disk Quota Information for a Single User?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I retrieve disk quota information for a single user?<BR><BR>&#8212; AB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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\u2019re running Windows XP or Windows 2003, you can retrieve disk quota information for a user via the Win32_DiskQuota class. For example, here\u2019s a script that retrieves disk quota information for user kenmyer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colQuotas = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_DiskQuota Where &#8221; &amp; _\n        &#8220;User=&#8217;Win32_Account.Domain=&#8221;&#8221;fabrikam&#8221;&#8221;,Name=&#8221;&#8221;kenmyer&#8221;&#8221;&#8216;&#8221;)<\/p>\n<p>For Each objQuota in colQuotas\n    Wscript.Echo &#8220;Disk Space Used: &#8221; &amp; objQuota.DiskSpaceUsed\n    Wscript.Echo &#8220;Limit: &#8221; &amp; objQuota.Limit \n    Wscript.Echo &#8220;Quota Volume: &#8221; &amp; objQuota.QuotaVolume  <\/p>\n<p>    intStatus = objQuota.Status \n    Select Case intStaus \n        Case 0 \n            strStatus = &#8220;OK&#8221;\n        Case 1\n            strStatus = &#8220;Warning limit reached&#8221;\n        Case 2\n            strStatus = &#8220;Quota exceeded&#8221;\n        Case Else\n            strStatus = &#8220;Unknown&#8221;\n     End Select<\/p>\n<p>    Wscript.Echo &#8220;Status: &#8221; &amp; strStatus \n    Wscript.Echo &#8220;Warning Limit: &#8221; &amp; objQuota.WarningLimit\nNext\n<\/PRE>\n<P>OK, granted, the WQL query looks a little crazy (all those double quote marks!), but that\u2019s 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\u2019s 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\u2019s easy; just replace <B>kenmyer<\/B> with <B>carolphilips<\/B> and <B>fabrikam<\/B> with <B>contoso<\/B>:<\/P><PRE class=\"codeSample\">Set colQuotas = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_DiskQuota Where &#8221; &amp; _\n        &#8220;User=&#8217;Win32_Account.Domain=&#8221;&#8221;contoso&#8221;&#8221;,Name=&#8221;&#8221;carolphilips&#8221;&#8221;&#8216;&#8221;)\n<\/PRE>\n<P>And at least the rest of the script is pretty straightforward; we just echo the values of the various properties. The only \u201cfancy\u201d thing we\u2019ve 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 <B>2<\/B>, the script converts that to the string <B>Quota exceeded<\/B>. 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.<\/P>\n<P>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 <I>had<\/I> to ask that, didn\u2019t you? This can be done; it\u2019s just that the WQL query looks even weirder due to the fact that the QuotaVolume property is yet another reference property:<\/P><PRE class=\"codeSample\">Set objQuota = objWMIService.Get _\n    (&#8220;Win32_DiskQuota.QuotaVolume=&#8217;Win32_LogicalDisk.DeviceID=&#8221;&#8221;C:&#8221;&#8221;&#8216;,&#8221; &amp; _\n        &#8220;User=&#8217;Win32_Account.Domain=&#8221;&#8221;tomservo&#8221;&#8221;,Name=&#8221;&#8221;Administrators&#8221;&#8221;&#8216;&#8221;)\n<\/PRE>\n<P>If you need to get information for a different drive, then just copy the preceding query and replace <B>C:<\/B> with the appropriate drive letter.<\/P>\n<P>Of course, while this script works great on Windows XP and Windows 2003, it doesn\u2019t do you much good on Windows 2000; that\u2019s because the Win32_DiskQuota class isn\u2019t 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\u2019t support disk quotas, period.) So what do you do if you need to get disk quota information for a Windows 2000 computer?<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">Set colDiskQuotas = CreateObject(&#8220;Microsoft.DiskQuota.1&#8221;)\ncolDiskQuotas.Initialize &#8220;C:\\&#8221;, True\nSet objUser = colDiskQuotas.FindUser(&#8220;fabrikam\\kenmyer&#8221;)<\/p>\n<p>Wscript.Echo &#8220;Logon name: &#8221; &amp; objUser.LogonName\nWscript.Echo &#8220;Quota limit: &#8221; &amp; objUser.QuotaLimit\nWscript.Echo &#8220;Quota threshold: &#8221; &amp; objUser.QuotaThreshold\nWscript.Echo &#8220;Quota used: &#8221; &amp; objUser.QuotaUsed\n<\/PRE>\n<P>As you can see, this is actually a shorter, more elegant-looking script, and with relatively few double quote marks. So why don\u2019t 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\u2019t connect to a remote machine and return disk quota information. <\/P>\n<P>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\u2019ll 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.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I retrieve disk quota information for a single user?&#8212; AB 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\u2019re running Windows XP or Windows 2003, you can retrieve [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[708,3,12,5],"class_list":["post-70933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disk-quotas","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I retrieve disk quota information for a single user?&#8212; AB 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\u2019re running Windows XP or Windows 2003, you can retrieve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70933"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70933\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}