Tuesday, July 19, 2016

Calculating Windows Disk Usage

WinDirStat (http://windirstat.info) is my favorite graphical tool for analyzing a disk drive when it gets full, but how do I know one is getting full?  The little snippet of code below uses the "Windows Management Instrumentation Command-line" (WMIC) to find the size and free space on a disk (C: in this example), then manipulate the result into the drive's size, used space, free space, and most importantly, the percentage in use.  

For example, you can use this to create a scheduled daily job that will send you an email if the percentage rises above 80%, giving you plenty of time to investigate before it becomes a real problem.

https://msdn.microsoft.com/en-us/library/bb742610.aspx

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get Size /format:value`) do set Size=%%x

set FreeMB=%FreeSpace:~0,-6%
set SizeMB=%Size:~0,-6%
set /a UsedMB=SizeMB-FreeMB
set /a Percentage=100 * UsedMB / SizeMB

No comments:

Post a Comment