Access to a disk drive using volume ID in batch

Discussion in 'Scripting' started by mephistooo2, Jun 2, 2022.

  1. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,694
    60
    #21 AveYo, Jun 6, 2022
    Last edited: Jun 7, 2022
    Above won't work for unmounted volumes, there's no bulletproof correlation between disks (physical) and volumes (software) since volumes can spawn multiple disks for dynamic, sparse, raid etc.

    With that in mind, to "find the disc number of a disc whose volume label we know", windows 11,10,8 is needed (edit: namespace not supported on 7):

    Code:
    
    set "label=MEDIA"
    
    set q1= $Q1=(gwmi Win32_Volume -filter $('Label='+[char]34+$env:label+[char]34)).DeviceID
    set q2= $Q2=(gwmi MSFT_Partition -Namespace root\Microsoft\Windows\Storage ^|where {$_.AccessPaths -contains $Q1}).DiskNumber
    for /f %%s in ('powershell -nop -c "%q1%; if ($Q1) {%q2%; $Q2}"') do set disk=%%s
    
    echo %disk%
    
    pause
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...