Windows command line to get physical drive size in GB
Calculations in cmd with set /A are limited to signed 32bit numbers, so you need vbscript or PowerShell to do the math.
Instead of using batch, wmic and vbscript I have here a hybrid batch/vbscript file which eliminates the need to create temporary files. A drawback is an echoed rem &
which is required to have both script versions accept the file.
Save this as a .cmd/.bat file
rem^ &@cls&cscript //nologo //e:vbscript "%~f0"&Pause&exit /b
On Error Resume Next
sComputer = "."
Set oWMI = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set cItems = oWMI.ExecQuery("Select * from Win32_DiskDrive",,48)
sOut = ""
For Each oItem in cItems
sOut = sOut & JL(26,oItem.Model) & JL(20,oItem.Name) & "Size=" & _
JR(8,FormatNumber(cdbl(oItem.Size)/1024/1024/1024, 2)) & " (GiB)" &vbCRLF
Next
Set cItems = Nothing
wscript.echo sOut
Function JL(Places, Value) ' Justify Left within Places
If len (Value) < Places then
JL=Left(Value & Space(Places), Places)
Else
JL=Value
End If
end Function
Function JR(Places, Value) ' Justify Right within Places
If Len (Value) < Places then
JR = Right(Space(Places) & Value, Places)
Else
JR=Value
End If
end Function
Output on my system (german locale)
Hitachi HDS722020ALA330 \\.\PHYSICALDRIVE2 Size=1.863,01 (GiB)
VB0250EAVER \\.\PHYSICALDRIVE0 Size= 232,88 (GiB)
ST2000DL003-9VT166 \\.\PHYSICALDRIVE1 Size=1.863,01 (GiB)
Drücken Sie eine beliebige Taste . . .
Related videos on Youtube
LadyDextrous
I work as a Computer Forensics consultant and consider my self fairly decent in logic though amateur in coding languages. Spend most of my time forensics investigations but off late trying my hands on automation :)
Updated on September 18, 2022Comments
-
LadyDextrous over 1 year
I want to display all the attached diskdrive model, name, index and size in command line I am using the below simple code
wmic diskdrive GET index,caption,name,size /format:table
Output
Caption Index Name Size SAMSUNG SSD PM830 2.5" 7 0 \\.\PHYSICALDRIVE0 256052966400 Seagate backup plus 1 \\.\PHYSICALDRIVE1 536870912000
I want the same output as the above except that i would like the size to be displayed in GB
I think there is some way to do that using for loops and temp vb files.
I am specifically looking for windows cmd code. Can anybody help?
I am able to do the size conversion and display it on cmd
for /f "tokens=1,2" %a in ('WMIC DISKDRIVE GET Name^,Size ^|FINDSTR /I /V "Name"') do @echo wsh.echo "%a" ^& " size=" ^& FormatNumber^(cdbl^(%b^)/1024/1024/1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%b"=="" @echo( & @cscript //nologo %temp%\tmp.vbs & del %temp%\tmp.vbs
Output
\\.\PHYSICALDRIVE0 Size=238.47 (GB)
But i am still not able to display the model name correctly.. I am just not able to get my for loop to run as intended (in my case the model name has spaces in between The below code works to just get the models printed.
for /f "tokens=1-5 skip=1" %a in ('wmic diskdrive GET model') do @echo. %a %b %c %d %e
Output
SAMSUNG SSD PM830 2.5" 7
Can someone help me in combining both the codes such that it displays like
SAMSUNG SSD PM830 2.5" 7 \\.\PHYSICALDRIVE0 Size=238.47 (GB)
-
Seth over 7 years
-
TOOGAM over 7 yearsSure, it can be done. You can send WMIC's output to a file (using something like /OUTPUT: or by redirection) and have VBScript read the file. Or, you could just have VBScript interact with WMI (instead of using WMIC), which might be easier. Once you get the numbers into VBScript, division ought to be simple.
-