How to list physical drives?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

How to list physical drives?

Post by MigrationUser »

01 Jan 2015 11:27
emil7768

How can i list all drives so they show in command prompt like this: \Device\Harddisk0\Partition2

----------------------------

#2 01 Jan 2015 11:54
npocmaka


Save this with .bat/.cmd extension:

Code: Select all

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
cscript //E:JScript //nologo "%~f0"
exit /b

// Sources of wisdom:
//http://social.msdn.microsoft.com/Forums/vstudio/en-US/659030c8-bcf5-4542-bbc6-eaf9679e090a/cannot-create-object-wmi-in-javascript
//http://blogs.technet.com/b/heyscriptingguy/archive/2005/05/23/how-can-i-correlate-logical-drives-and-physical-disks.aspx
//http://stackoverflow.com/a/1144788/388389

************** end of JSCRIPT COMMENT **/


String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};


var winmgmts= GetObject("winmgmts:\\\\.\\root\\cimv2")
var drives = winmgmts.ExecQuery( "SELECT * FROM Win32_DiskDrive", null, 48 );
//WScript.Echo(drives);

var drvs = new Enumerator(drives);
for (;!drvs.atEnd();drvs.moveNext()) {
    var drive=drvs.item();

    WScript.Echo( "Physical Disk: " + drive.Caption + " -- " + drive.DeviceID );
    var deviceID = drive.DeviceID.replaceAll( "\\" ,"\\\\");
    var colPartitions = winmgmts.ExecQuery( "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=\"" + 
        deviceID + "\"} WHERE AssocClass =  Win32_DiskDriveToDiskPartition" , null, 48 );

    var colParts = new Enumerator(colPartitions);
    for (;!colParts.atEnd();colParts.moveNext()) {

        var partition=colParts.item();

        //WScript.Echo( "Disk Partition: " + partition.DeviceID );
        var colLogicalDisks = winmgmts.ExecQuery( "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=\"" +
                partition.DeviceID + "\"} WHERE AssocClass = Win32_LogicalDiskToPartition" , null, 48);
        var colLD = new Enumerator(colLogicalDisks);

        if (typeof colLD.item() != "undefined") {
            for (;!colLD.atEnd();colLD.moveNext()) {
                var logicalDisk=colLD.item();
                WScript.Echo( "  Logical Disk: " + logicalDisk.DeviceID + " Disk Partition:  harddisk" + partition.DeviceID.split("#")[1].split(",")[0] + "\\partition" + partition.DeviceID.split("#")[2] );
            }
        } else {
            WScript.Echo( "  Disk Partition: harddisk" + partition.DeviceID.split("#")[1].split(",")[0] + "\\partition" + partition.DeviceID.split("#")[2] );
        }

    }   

}
I've created this to answer this . You can modify the output to apply to your needs

----------------------------

#3 01 Jan 2015 12:44
emil7768

Thanks, but i was hoping for more simpler solution, a few commands that i can run in command prompt. Few day ago when i played with TrueCrypt i remember getting a list of the partitions in the way i am asking, but now i can't remember what commands i used. I am not very advanced with using the command line.

----------------------------

#4 01 Jan 2015 13:15
npocmaka

what about fltmc volumes (it requires admin permissions)

----------------------------

#5 01 Jan 2015 13:49
emil7768

fltmc volumes doesn't seem to list all partitions. I have one encrypted partition and it doesn't show up when i run the command.

----------------------------

#6 02 Jan 2015 10:22
bluesxman

How about...

Code: Select all

wmic volume get caption,capacity,label,filesystem
cmd | *sh | ruby | chef

----------------------------

#7 02 Jan 2015 10:56
foxidrive

That works here for the volumes - it would be useful to know what the original poster was actually attempting to do.

IE: is he trying to find an encrypted drive that isn't mounted?

----------------------------

#8 03 Jan 2015 10:30
emil7768
bluesxman wrote:

How about...

Code: Select all

 wmic volume get caption,capacity,label,filesystem
This doesn't list the partitions in the way i need.
----------------------------

#9 03 Jan 2015 10:31
emil7768
foxidrive wrote:

That works here for the volumes - it would be useful to know what the original poster was actually attempting to do.

IE: is he trying to find an encrypted drive that isn't mounted?

I want to get the ARC path of the partitions, i think this is the right term for these paths.
----------------------------

#10 03 Jan 2015 11:11

foxidrive
emil7768 wrote:

I want to get the ARC path of the partitions, i think this is the right term for these paths.
http://support.microsoft.com/kb/155222

It doesn't seem to be what your examples were.

Last edited by foxidrive (03 Jan 2015 11:12)

----------------------------

#11 03 Jan 2015 11:30
bluesxman


I believe this does the exact thing you requested:

Code: Select all

@echo off

for /f "usebackq tokens=2,4 delims=,# " %%a in (`wmic partition get name ^| findstr /v /b "Name"`) do (
  echo \Device\Harddisk%%a\Parition%%b
)
... Now mapping these back to such useful things as drive letters, that seems to be tricky for some reason; I've not been able to find a way to correlate as such using WMIC commands.

Last edited by bluesxman (03 Jan 2015 11:44)

cmd | *sh | ruby | chef

----------------------------

#12 03 Jan 2015 11:47

bluesxman

"diskpart" may be able to help us tie it back to drive letters (this isn't finished code, just showing what info can be gleaned)

Code: Select all

@echo off
(
for %%a in (
"list disk"
"list volume"
"select disk=0"
"list partition"
) do (echo %%~a)
) > "%temp%\diskpart.txt"

diskpart /s "%temp%\diskpart.txt"
cmd | *sh | ruby | chef

----------------------------

#13 03 Jan 2015 20:15

emil7768

Thanks, but the scripts doesn't give the output i want.

----------------------------

#14 05 Jan 2015 15:35
bluesxman


Can you be a bit more specific about what output you are requiring?

From my understanding of your opening question, this script will (I believe) give the display of partitions in the format you requested

Code: Select all

@echo off

for /f "usebackq tokens=2,4 delims=,# " %%a in (`wmic partition get name ^| findstr /v /b "Name"`) do (
  echo \Device\Harddisk%%a\Parition%%b
)
Other tools/strategies may be required to make that more useful (such as "diskpart" as noted above).

cmd | *sh | ruby | chef

----------------------------

#15 05 Jan 2015 19:47
foxidrive
bluesxman wrote:

this script will (I believe) give the display of partitions in the format you requested
I'm not sure why wmic shows stuff like this - my drives don't have two partitions - this is part of a spanned drive and each drives shows the two partitions.

\Device\Harddisk4\Parition0
\Device\Harddisk4\Parition1

Last edited by foxidrive (05 Jan 2015 19:48)

----------------------------

#16 06 Jan 2015 07:58

bluesxman

I can only go by what I have in front of me -- in my laptop I have a single Basic physical drive with 3 partitions; in this scenario wmic gives an accurate view.

cmd | *sh | ruby | chef

----------------------------

#17 06 Jan 2015 20:33

emil7768
bluesxman wrote:

Can you be a bit more specific about what output you are requiring?

From my understanding of your opening question, this script will (I believe) give the display of partitions in the format you requested

Code: Select all

    @echo off

    for /f "usebackq tokens=2,4 delims=,# " %%a in (`wmic partition get name ^| findstr /v /b "Name"`) do (
      echo \Device\Harddisk%%a\Parition%%b
    )
Other tools/strategies may be required to make that more useful (such as "diskpart" as noted above).
Sorry, i mean the partition paths with the corresponding letters. This are my partitions:

Code: Select all

DISKPART> list partition

  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
  Partition 1    Primary             15 GB  1024 KB
  Partition 2    Primary           3416 MB    15 GB
  Partition 0    Extended           129 GB    19 GB
  Partition 3    Logical             62 GB    19 GB
  Partition 4    Logical             67 GB    81 GB

DISKPART> list volume

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     F                       DVD-ROM         0 B  No Media
  Volume 1     C                NTFS   Partition     15 GB  Healthy    System
  Volume 2     G                RAW    Partition   3416 MB  Healthy
  Volume 3     D   Local Disc   NTFS   Partition     62 GB  Healthy
  Volume 4     E                NTFS   Partition     67 GB  Healthy
This is the output i get from the script you posted:

Code: Select all

E:\Desktop>test.bat
\Device\Harddisk0\Parition0
\Device\Harddisk0\Parition1
\Device\Harddisk0\Parition2
----------------------------

#18 07 Jan 2015 02:42

Aacini

This is funny: you have said us several times that you want NOT the solutions provided, but you didn't said what IS the result you want! Simple question: based on your previous DISKPART output example, what exactly is the output you want?

Please, note that "the output you want" must be based on information provided by existent commands; otherwise, how we could generate it?

----------------------------

#19 07 Jan 2015 09:30

emil7768

I need the output to show partition paths with the corresponding letters. For example:

Code: Select all

C:  \Device\Harddisk0\Parition0
E:  \Device\Harddisk0\Parition1
D:  \Device\Harddisk0\Parition2
My question is if there is a command that can provide that kind of output?

----------------------------

#20 07 Jan 2015 19:31

Aacini
emil7768 wrote:

I need the output to show partition paths with the corresponding letters. For example:

Code: Select all

    C:  \Device\Harddisk0\Parition0
    E:  \Device\Harddisk0\Parition1
    D:  \Device\Harddisk0\Parition2
My question is if there is a command that can provide that kind of output?
I don't know. What I know is that you may get that output from the "DISKPART list volume" output you show before via this Batch file:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Insert code to get the output of "DISKPART list volume" into DiskPartOut.txt file here

set i=0
for /F "tokens=3" %%a in ('findstr "NTFS" DiskPartOut.txt') do (
   echo %%a:  \Device\Harddisk0\Partition!i!
   set /A i+=1
)
However, the numeration of the partitions you show above is wrong; the right one is this based on the "DISKPART list partition" output:

Code: Select all

C:  \Device\Harddisk0\Partition1
D:  \Device\Harddisk0\Partition3
E:  \Device\Harddisk0\Partition4
I also have the code to show the right number of partition, if you want it...

Antonio

Last edited by Aacini (07 Jan 2015 19:34)

----------------------------

#21 08 Jan 2015 09:57
emil7768
Aacini wrote:

I don't know. What I know is that you may get that output from the "DISKPART list volume" output you show before via this Batch file:

Code: Select all

    @echo off
    setlocal EnableDelayedExpansion

    rem Insert code to get the output of "DISKPART list volume" into DiskPartOut.txt file here

    set i=0
    for /F "tokens=3" %%a in ('findstr "NTFS" DiskPartOut.txt') do (
       echo %%a:  \Device\Harddisk0\Partition!i!
       set /A i+=1
    )
I tried to run the script but i got this error:

E:\Desktop>test.bat
FINDSTR: Cannot open DiskPartOut.txt

----------------------------

#22 08 Jan 2015 10:10
foxidrive
emil7768 wrote:

I tried to run the script but i got this error:

E:\Desktop>test.bat
FINDSTR: Cannot open DiskPartOut.txt
There's a hint in the script about that...

----------------------------

#23 08 Jan 2015 11:53
Simon Sheppard


Heres a VBScript thats close to what you want:

Code: Select all

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
 
For Each objDrive In colDiskDrives
    Wscript.Echo "Physical Disk: " & objDrive.Caption & " -- " & objDrive.DeviceID 
    strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
    Set colPartitions = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
            strDeviceID & """} WHERE AssocClass = " & _
                "Win32_DiskDriveToDiskPartition")
 
    For Each objPartition In colPartitions
        Wscript.Echo "Disk Partition: " & objPartition.DeviceID
        Set colLogicalDisks = objWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                objPartition.DeviceID & """} WHERE AssocClass = " & _
                    "Win32_LogicalDiskToPartition")
 
        For Each objLogicalDisk In colLogicalDisks
            Wscript.Echo "Logical Disk: " & objLogicalDisk.DeviceID
        Next
        Wscript.Echo
    Next
    Wscript.Echo
Next
From the Scripting Guys blog

----------------------------

#24 09 Jan 2015 09:44
emil7768
foxidrive wrote:

emil7768 wrote:

I tried to run the script but i got this error:

E:\Desktop>test.bat
FINDSTR: Cannot open DiskPartOut.txt

There's a hint in the script about that...
If you are referring to this hint i am not sure how to interpret it:

rem Insert code to get the output of "DISKPART list volume" into DiskPartOut.txt file here

I don't have any experience with scripting.

----------------------------

#25 09 Jan 2015 09:50
emil7768
Simon Sheppard wrote:

Heres a VBScript thats close to what you want:

Code: Select all

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
     
    For Each objDrive In colDiskDrives
        Wscript.Echo "Physical Disk: " & objDrive.Caption & " -- " & objDrive.DeviceID 
        strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
        Set colPartitions = objWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
                strDeviceID & """} WHERE AssocClass = " & _
                    "Win32_DiskDriveToDiskPartition")
     
        For Each objPartition In colPartitions
            Wscript.Echo "Disk Partition: " & objPartition.DeviceID
            Set colLogicalDisks = objWMIService.ExecQuery _
                ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                    objPartition.DeviceID & """} WHERE AssocClass = " & _
                        "Win32_LogicalDiskToPartition")
     
            For Each objLogicalDisk In colLogicalDisks
                Wscript.Echo "Logical Disk: " & objLogicalDisk.DeviceID
            Next
            Wscript.Echo
        Next
        Wscript.Echo
    Next
From the Scripting Guys blog
I don't think the script is working. It just popup number of windows, some of them just empty, only with one OK button.

----------------------------

#26 09 Jan 2015 11:26
Simon Sheppard


^ Sorry I should have made it clearer, you have to run VBScript via CSCRIPT.exe

----------------------------

#27 09 Jan 2015 16:36
emil7768
Simon Sheppard wrote:

^ Sorry I should have made it clearer, you have to run VBScript via CSCRIPT.exe

Now it is working. This is the output i got:

Code: Select all

E:\Desktop>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Physical Disk: Hitachi HTS542516K9SA00 ATA Device -- \\.\PHYSICALDRIVE0
Disk Partition: Disk #0, Partition #0
Logical Disk: C:

Disk Partition: Disk #0, Partition #1
Logical Disk: G:

Disk Partition: Disk #0, Partition #2
Logical Disk: D:
Logical Disk: E:
----------------------------

#28 22 Mar 2015 17:54
Giacomo

Dunno if this can be useful to him or someone.

Code: Select all

@ECHO OFF
::
:: Start delayed expansion MountedDrives, no end
::
SETLOCAL EnableDelayedExpansion
::
:: Delims : strips out :\ characters
:: First statement catenate variables
:: Second statement substitute 8 spaces with a comma
::
FOR /F "DELIMS=:" %%M IN ('MOUNTVOL ^|FIND ":\"') DO (
 SET TempMountedDrives=!TempMountedDrives!%%M
 SET TempMountedDrives=!TempMountedDrives:        =,!
)
::
:: Strip first character
::
SET MountedDrives=%TempMountedDrives:~1%
::
:: Show output
::
SET MountedDrives&&PAUSE
Output

MountedDrives=C,D,A
Press any key to continue ...
Post Reply