Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I found the below code to do almost exactly what I want but for every drive in my system. I only want to get it for one specific drive. Is there any way to modify the following

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDiskPartitions = objWMIService.ExecQuery _
    ("Select * from Win32_DiskPartition where ")
For each objPartition in colDiskPartitions
    Wscript.Echo "Block Size: " & vbTab &  objPartition.BlockSize     
    Wscript.Echo "Bootable: " & vbTab &  objPartition.Bootable 
    Wscript.Echo "Boot Partition: " & vbTab &  objPartition.BootPartition
    Wscript.Echo "Description: " & vbTab &  objPartition.Description
    Wscript.Echo "Device ID: " & vbTab &  objPartition.DeviceID       
    Wscript.Echo "Disk Index: " & vbTab &  objPartition.DiskIndex     
    Wscript.Echo "Index: " & vbTab &  objPartition.Index       
    Wscript.Echo "Name: " & vbTab &  objPartition.Name 
    Wscript.Echo "Number Of Blocks: " & vbTab &  _
        objPartition.NumberOfBlocks     
    Wscript.Echo "Primary Partition: " & vbTab &  _
        objPartition.PrimaryPartition   
    Wscript.Echo "Size: " & vbTab &  objPartition.Size 
    Wscript.Echo "Starting Offset: " & vbTab &  _
        objPartition.StartingOffset     
    Wscript.Echo "Type: " & vbTab &  objPartition.Type 
Next
Posted

1 solution

Win32_DiskPartition identifies partitions by Disk starting at zero followed by partition number on the Disk starting at 0.

To get the map of partitions to drives use the Win32_LogicalDiskTopartition, parameter Dependant will contain the drives letter Antecedent will contain the Disk and Partition numbers for that drive letter.

Extract the "Disk #x, Partition #x" from the Antecedant.

So if I want info for drive C: when Dependent contains C: then Antecedent contains the disk and partition e.g.

Antecedent: \\mypcname\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #1"
Dependent: \\mypcname\root\cimv2:Win32_LogicalDisk.DeviceID="C:"

Now you can take the "Disk #0, Partition #1" and use it in your Win32_DiskPartitions query.

"SELECT * FROM Win32_DiskPartition WHERE Name='Disk #0, Partition #1'"

And that will be the information for drive C:

I recommend you use the WMICodeCreator tool from Microsoft to quickly test out WMI commands, it also writes the code for you so you can cut and paste.
 
Share this answer
 
v2
Comments
johnjsm 19-Aug-15 14:54pm    
This is cool but what if the information I need is for a USB that is attached. Is there a case where if I know that the drive letter is always going to be E for example is there a way to then get the partition information for that
Michael_Davies 19-Aug-15 14:57pm    
I have already told you how to get info for a specific drive.

You need to ask the right question, nowhere in your question do you mention that it is a removable USB drive.

Use a famous search engine! There are even articles on CodeProject on the matter.

http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives
johnjsm 19-Aug-15 14:59pm    
Sorry

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900