Windows Command line get disk space in GB

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).


Solution 1:

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

==>

Solution 2:

I realize you're looking as VBS right now, but PowerShell can do this very easily:

$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size, FreeSpace

Write-Host ("{0}GB total" -f [math]::truncate($disk.Size / 1GB))
Write-Host ("{0}GB free" -f [math]::truncate($disk.FreeSpace / 1GB))

First line gets the disk info from WMI (just does C: in this example), and selects just the Free Space and Total sizes.

In the next two lines write the Free Space and Total sizes to the console, formatted to be in GB, with fractions truncated off.

Example output (as-is):

223GB total
125GB free

Solution 3:

Divide by 1024 to get kibytes (2^10). Divide by 1024 x 1024 to get Mibytes (2^20). Divide by 1024 x 1024 x 1024 to get Gibytes (2^30).

Windows calls these units kilobytes, Megabytes, and Gigabytes for historical reasons. I think my casing is right.

So change your sums.