WMI Query to return FreeSpace, Drive name and Size of disk in Windows server

H All,

I am executing below wmi query to get the disk information from windows server.

Select Name, FreeSpace, Size from Win32_LogicalDisk

The output i am getting is a xml file.

What we are expecting is the output of wmi query to be below. Can this be achieved.

"53142114304|C:|110100705280|"

"387322912768|D:|543049400320|"

Or is there an script to achieve this.

Any help is appreciated.

Thanks and Regards
Riyas


May 12th, 2013 8:01am

Here is a VBScript a wrote a little while ago:

'--------------------------------------------------- 'Get the properties of all partitions on all drives, 'including USB drives. 'Core code from the Scripting Guy '7.1.2012 FNL '--------------------------------------------------- Set oWMIService = GetObject("winmgmts:\\.\root\cimv2") Set cDiskDrives = oWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive") For Each oDrive In cDiskDrives WScript.echo "Disk #" & oDrive.Index & "(" & oDrive.InterfaceType & "): " _ & oDrive.Caption & ", Size=" & Format(oDrive.Size, False) & " MBytes" WScript.echo "Part. Drive F/S Size(MBytes) Free(MBytes) Active Primary" WScript.echo String(71, "-") Set cPartitions = oWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" _ & Replace(oDrive.DeviceID, "\", "\\") & """} WHERE AssocClass = " & "Win32_DiskDriveToDiskPartition") For Each oPartition In cPartitions aPartition = Split(oPartition.DeviceID) Set cLogicalDisks = oWMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & oPartition.DeviceID _ & """} WHERE AssocClass = Win32_LogicalDiskToPartition") if oPartition.Bootable then sActive = "Yes" Else sActive = "No " if oPartition.PrimaryPartition then sPrimary = "Yes" Else sPrimary = "No " For Each oLogicalDisk In cLogicalDisks For Each oVolume In cLogicalDisks sLabel = Left(oVolume.VolumeName & Space(12), 12) Next With oLogicalDisk sSpacer = Space(8 - Len(.FileSystem)) WScript.Echo " " & aPartition(3) & " " & .DeviceID & " " & sLabel _ & .FileSystem & sSpacer & Format(.Size, True) & " " _ & Format(.FreeSpace, True) & " " & sActive & " " & sPrimary End With Next Next WScript.Echo Next Function Format(n, bPad) n = FormatNumber(n/1000000, 0, -1, 0, -1) if bPad then Format = Space(7-Len(n)) & n Else Format = n End Function

It generates tabular output like this:

Disk #0(IDE): Hitachi HTS545050B9A300 ATA Device, Size=500,105 MBytes
Part.  Drive          F/S   Size(MBytes)  Free(MBytes)  Active  Primary
-----------------------------------------------------------------------
 #0    C: System      NTFS     73,400        33,398     Yes     Yes
 #1    D: Documents   NTFS    104,858        42,572     No      Yes
 #2    E: Drivers     NTFS    321,740       252,471     No      Yes

Disk #1(USB): Creative NOMAD MuVo TX USB Device, Size=123 MBytes
Part.  Drive          F/S   Size(MBytes)  Free(MBytes)  Active  Primary
-----------------------------------------------------------------------
 #0    G: Revision    NTFS        126            97     Yes     Yes


Free Windows Admin Tool Kit Click here and download it now
May 12th, 2013 8:17am

If you don't mind a bit of PowerShell:

$Fs=@{Label='Free Space'; expression={$_.freespace};formatstring='n0'}
$Sz=@{Label='Size'; expression={$_.Size};formatstring='n0'}
Get-WMIObject win32_logicaldisk| Format-Table Name, $Fs, $Sz  -a

The first two statements create hash tables passed to format-table to ensure prettier output. Here is the result on My machine:

Name      Free Space              Size
----      ----------              ----
C:    99,286,458,368   210,005,651,456
D:     1,946,718,208     2,150,797,312
E:    75,485,503,488   537,918,435,328
F:
G:
H:
I:
J:
K:
M:   238,193,594,368 2,000,390,451,200
N:   799,358,926,848 2,000,390,451,200
Q:   224,305,520,640   249,892,433,920
V:   351,631,953,920   998,904,950,784

You could go further, like this:

$Fs=@{Label='Free Space (GB)'; expression={($_.freespace)/1gb};formatstring='n2'}
$Sz=@{Label='Size (GB)'; expression={($_.Size)/1gb};formatstring='n2'}
Get-WMIObject win32_LogicalDisk| Format-Table Name, $fs, $Sz  -a

May 12th, 2013 10:26am

What we are expecting is the output of wmi query to be below. Can this be achieved.

"53142114304|C:|110100705280|"

"387322912768|D:|543049400320|"



gwmi win32_logicaldisk|%{
    if($_.size){
        "$($_.freespace)"+'|'+$_.name+'|'+$_.size
}}

Free Windows Admin Tool Kit Click here and download it now
May 13th, 2013 1:35am

Hi Frederik,

I'm currently working on vbscript to report disk, partition, and logical drive usage and size data, in an exact way, in bytes. I use several WMI queries similar the ones you've used here. And in some certain circumstances I've got interesting results. Just like you in your attached example output above:

Disk#1(USB)...Size=123 MBytes, Partition #0... size 126 MBytes. ???

In my experiences I've got similar results from disks provided by the virtualisation layer, or by SAN connections.

For example:

Win32_LogicalDiskToPartition shows:

Antecedent       : \\xxxxx\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0"
Dependent        : \\xxxxx\root\cimv2:Win32_LogicalDisk.DeviceID="T:"
EndingAddress    : 1099510579199
StartingAddress  : 1048576

from this data, the size should be 1 099 509 530 624 bytes and the EndingAddress: 1 099 510 579 199

the corresponding Win32_DiskPartition shows:

NumberOfBlocks   : 2147479552
BootPartition    : False
Name             : Disk #2, Partition #0
PrimaryPartition : True
Size             : 1099509530624
Index            : 0

which is Ok, but the corresponding Win32_DiskDrive shows:

Partitions : 1
DeviceID   : \\.\PHYSICALDRIVE2
Model      : IBM 2145  Multi-Path Disk Device
Size       : 1099506078720
Caption    : IBM 2145  Multi-Path Disk Device

The size is: 1 099 506 078 720

So the partition is greater then the disk.

From the MSDN library: https://msdn.microsoft.com/en-us/library/aa394132%28v=vs.85%29.aspx

Size
Data type: uint64
Access type: Read-only
Qualifiers: Units (Bytes)

Size of the disk drive. It is calculated by multiplying the total number of cylinders, tracks in each cylinder, sectors in each track, and bytes in each sector.

For more information about using uint64 values in scripts, see Scripting in WMI.

TotalCylinders
Data type: uint64
Access type: Read-only

Total number of cylinders on the physical disk drive. Note: the value for this property is obtained through extended functions of BIOS interrupt 13h. The value may be inaccurate if the drive uses a translation scheme to support high-capacity disk sizes. Consult the manufacturer for accurate drive specifications.

Example: 657

For more information about using uint64 values in scripts, see Scripting in WMI.

By the above property definitions, the disk size can be inaccurate.

Do you know a way to get the accurate disk size using wmi, or any script technique?

May 21st, 2015 3:52am

Do you know a way to get the accurate disk size using wmi, or any script technique?

Sorry, this thread was closed two years ago. Please start a thread of your own.
Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 7:49am

Thanks Thomas!
September 4th, 2015 8:20am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics