{"id":63803,"date":"2004-10-13T19:39:00","date_gmt":"2007-10-15T04:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/15\/how-can-i-determine-the-percentage-of-free-space-on-a-drive\/"},"modified":"2004-10-13T19:39:00","modified_gmt":"2007-10-15T04:00:00","slug":"how-can-i-determine-the-percentage-of-free-space-on-a-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-percentage-of-free-space-on-a-drive\/","title":{"rendered":"How Can I Determine the Percentage of Free Space on a Drive?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I know how to determine the amount of free space on a drive, but how can I determine the <I>percentage<\/I> of free disk space on a drive?<BR><BR>&#8212; MS<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JP. You\u2019re right: determining the amount of free disk space on a drive is pretty easy. For example, this simple little script tells you the amount of free space (in bytes) for drive C on a computer:<\/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;)\nSet colDisks = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_LogicalDisk Where DeviceID = &#8216;C:'&#8221;)\nFor Each objDisk in colDisks\n    Wscript.Echo objDisk.FreeSpace\nNext\n<\/PRE>\n<P>And we have good news for you: determining the percentage of free disk space is almost as easy. That\u2019s because the percentage of free disk space requires just a simple calculation: you take the available disk space and divide it by the total size of the drive. Suppose we had a <I>really<\/I> small drive, a 10-byte drive with 2 bytes of free disk space. To determine the percentage of free disk space, we\u2019d divide 2 by 10 (free space by total size) and get a value of .20. In other words, 20% free space on our teeny-tiny drive.<\/P>\n<P>It\u2019s a simple equation, and we already have half of it: the FreeSpace property of the Win32_LogicalDisk class tells us the amount of free space. And although you might not have realized it, we also have the second half of the equation: the Size property of that same class tells us the size of the drive. All we need to do in our script is divide the FreeSpace by the Size, and we have our percentage:<\/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;)\nSet colDisks = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_LogicalDisk Where DeviceID = &#8216;C:'&#8221;)\nFor Each objDisk in colDisks\n    intFreeSpace = objDisk.FreeSpace\n    intTotalSpace = objDisk.Size\n    pctFreeSpace = intFreeSpace \/ intTotalSpace\n    Wscript.Echo pctFreeSpace \nNext\n<\/PRE>\n<P>Nifty, huh? The only problem with this script is that it reports back a decimal number like 0.22457268101427. Technically accurate, yes, but an answer that doesn\u2019t really look much like a percentage. So let\u2019s use VBScript\u2019s FormatPercent function to get back a value like 22.45%:<\/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;)\nSet colDisks = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_LogicalDisk Where DeviceID = &#8216;C:'&#8221;)\nFor Each objDisk in colDisks\n    intFreeSpace = objDisk.FreeSpace\n    intTotalSpace = objDisk.Size\n    pctFreeSpace = intFreeSpace \/ intTotalSpace\n    Wscript.Echo FormatPercent(pctFreeSpace)\nNext\n<\/PRE>\n<P>That\u2019s <I>much<\/I> better.<\/P>\n<P>Just for the heck of it, here\u2019s another variation. By default, the Win32_LogicalDisk class looks at <I>all<\/I> disk drives on your computer, including floppy drives, removable Zip drives, etc. If you\u2019d like to get back free space information on just your hard drives, modify your WQL Query so that it selects all drives with a DriveType of 3 (which, in the world of WMI, represents a hard drive). In other words:<\/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;)\nSet colDisks = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_LogicalDisk Where DriveType = 3&#8221;)\nFor Each objDisk in colDisks\n    intFreeSpace = objDisk.FreeSpace\n    intTotalSpace = objDisk.Size\n    pctFreeSpace = intFreeSpace \/ intTotalSpace\n    Wscript.Echo objDisk.DeviceID, FormatPercent(pctFreeSpace)\nNext\n<\/PRE><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1013.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1013.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I know how to determine the amount of free space on a drive, but how can I determine the percentage of free disk space on a drive?&#8212; MS Hey, JP. You\u2019re right: determining the amount of free disk space on a drive is pretty easy. For example, this simple little script tells [&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":[216,3,12,5],"class_list":["post-63803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disk-drives-and-volumes","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I know how to determine the amount of free space on a drive, but how can I determine the percentage of free disk space on a drive?&#8212; MS Hey, JP. You\u2019re right: determining the amount of free disk space on a drive is pretty easy. For example, this simple little script tells [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63803","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=63803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63803\/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=63803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}