Jump to content
Sign in to follow this  

GetDriveLetter from DiskID

Recommended Posts

I want to read the assigned drive letter of a disk.

During runtime I only got the DiskID and now I want to check if there is an assigned drive letter.

(In my special case I will have disks with one partition, but the scritp below will find all ...)

I do not want to read any DISKPART so I tried it with this:

#include <Array.au3>
global $DiskPartitionLetter[26][2]
ConsoleWrite("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"&@LF&"Disk ID "&$DiskID&" returns this letter for first partition:" & $DiskPartitionLetter[1][1] & @LF)

func GetDriveLetterFromDiskId($DriveID)
$oWMISvc = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colDiskDrives = $oWMISvc.ExecQuery("SELECT * FROM Win32_DiskDrive")

    ConsoleWrite("DiskDrive = \\.\PHYSICALDRIVE" & $DriveID & "  Caption = USB BACK DRIVE"  & @LF)

    $sQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='\\.\PHYSICALDRIVE" & $DriveID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
    $colPartitions = $oWMISvc.ExecQuery($sQuery)
    For $oPartition In $colPartitions
        ConsoleWrite("Partition = " & $oPartition.DeviceId & @LF)

        $sQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $oPartition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
        $colLogicalDisks = $oWMISvc.ExecQuery($sQuery)

        For $oLogicalDisk In $colLogicalDisks
            ConsoleWrite("LogicalDisk = " & $oLogicalDisk.DeviceId & @LF)

    return $DiskPartitionLetter

Is there something better?

I thought I remember something like a "dll call" post, but I'm unable to find it now.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...