Windows Command line get disk space in GB

batchcommand linevbscriptwindows

I'm trying to create a batch file to pull the total size and free space of the C:\ drive of servers (locally run script). I also need the output to be easily readable, so bytes is not going to work, so I'm ok with having a command line that creates a temp .vbs file.

The following seems like it could work, but the formatting/math isn't correct.

setlocal
for /f "tokens=6" %a in ('fsutil volume diskfree C: ^| find "of bytes"') do set diskspace=%a
echo wsh.echo FormatNumber(cdbl(%diskspace%)/1024, 0) > %temp%.\tmp.vbs
for /f %a in ('cscript //nologo %temp%.\tmp.vbs') do set diskspace=%a
del %temp%.\tmp.vbs
echo For example %diskspace%

The above commands are also only showing free space… I would like total size too… Wondering if the following command might be better for pulling the info:

WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:"

Note also that I want this to be able to be copy/pasted directly into a command prompt (not a batch file – forced requirements). I've already removed the "%%"'s from the code above.

Note: Needs to run natively on Server 2003+ (so Powershell is out, as well as any 3rd party utils).

Best Answer

Under the not a batch file - forced requirements clause, next cmd one-liner could help:

for /f "tokens=1-3" %a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do @echo wsh.echo "%b" ^& " free=" ^& FormatNumber^(cdbl^(%a^)/1024/1024/1024, 2^)^& " GiB"^& " size=" ^& FormatNumber^(cdbl^(%c^)/1024/1024/1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%c"=="" @echo( & @cscript //nologo %temp%\tmp.vbs & del %temp%\tmp.vbs

Output:

==>for /f "tokens=1-3" %a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FIND
STR /I /V "Name"') do @echo wsh.echo "%b" ^& " free=" ^& FormatNumber^(cdbl^(%a^
)/1024/1024/1024, 2^)^& " GiB"^& " size=" ^& FormatNumber^(cdbl^(%c^)/1024/1024/
1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%c"=="" @echo( & @cscript //nolog
o %temp%\tmp.vbs & del %temp%\tmp.vbs

C: free=79,11 GiB size=111,45 GiB

D: free=929,47 GiB size=931,51 GiB

==>
Related Question