Get USB drive letter under WinPE 10

Discussion in 'Scripting' started by sebus, Feb 21, 2017.

  1. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,356
    2,026
    210
    #1 sebus, Feb 21, 2017
    Last edited by a moderator: Apr 20, 2017
    With normal installation of Windows 10 this one works perfectly fine:

    Code:
    @echo off
    
    for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 
    
    2^>NUL`) do (
    
    if %%l equ 2 (
    echo %%i is a USB drive.
    )
    )
    
    Code:
    C:\>usb
    D: is a USB drive.
    But under Windows PE 10 it returns nothing, even I get:

    Code:
    X:\windows\system32>wmic logicaldisk get caption^,description^,drivetype
    Caption  Description  DriveType
    C:                    3
    D:                    2
    E:                    5
    X:                    3
    
    Help appreciated

    sebus
     
  2. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,356
    2,026
    210
    #2 sebus, Feb 21, 2017
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Instead I used this, which works fine under WinPE as well

    Code:
    @Echo off
    setlocal enabledelayedexpansion
    Set "USB="
    REM get removable loaded drives:
    for /f "tokens=1-5" %%a in (
     'wmic logicaldisk list brief'
    ) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
    Echo:Usb removable drives:%USB%
    EndLocal&Set USB=%USB:~1%&Goto :Eof
     
  3. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,964
    907
    60
    #3 Flipp3r, Feb 21, 2017
    Last edited by a moderator: Apr 20, 2017
    When I boot into WinPE my key is always U drive. In Startnet.cmd I run FindUSB. FindUSB looks for "\Boot\WinPE64.wim" & changes the key letter to U drive.

    You can compile both 32 & 64bit with AutoIT3. Change what it looks for. Source for FindUSB.au3:
    Code:
    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Outfile=FindUSB.exe
    #AutoIt3Wrapper_Outfile_x64=FindUSB64.exe
    #AutoIt3Wrapper_Compile_Both=y
    #AutoIt3Wrapper_UseX64=y
    #AutoIt3Wrapper_Res_Language=1033
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    $MyUSB = ""
    $strComputer = @ComputerName
    Dim $objWMIService = objget("winmgmts:\\" & $strComputer & "\root\cimv2")
    Dim $colDevices = $objWMIService.ExecQuery ("Select * From Win32_logicaldisktopartition")
    For $objDevice in $colDevices
        $strDeviceName = $objDevice.antecedent
    $strdrive = $objDevice.dependent
    $strQuote = Chr(34)
    $strDriveName = StringReplace($strDrive, $strQuote, "")
    $arrDriveNames = stringSplit($strDriveName, "=")
        $strDeviceName = StringReplace($strDeviceName, $strQuote, "")
    $arrDeviceNames = stringSplit($strDeviceName, "=")
    $split = stringsplit($arrDeviceNames[2],",")
    $replace = Stringtrimleft($split[1], 6)
        Dim $colUSBDevices = $objWMIService.ExecQuery("Select * From Win32_diskdrive Where index = '" & $replace & "'")
      For $objUSBDevice in $colUSBDevices
      ;msgbox(0,"",$arrDriveNames[2] & "drive " & " is" & @lf & $objUSBDevice.caption)
      If FileExists($arrDriveNames[2] & "\Boot\WinPE64.wim") Then
    $MyUSB = $arrDriveNames[2]
    ExitLoop
    EndIf
    Next
    If $MyUSB > "" Then ExitLoop
    Next
    ;msgbox(0,"", $MyUSB)
    If $MyUSB = "" Then Exit
    $tmp="sel vol " & $MyUSB & @CRLF
    $tmp=$tmp & "remove" & @CRLF
    $tmp=$tmp & "assign letter=u" & @CRLF
    $MountFile=FileOpen("X:\Windows\System32\tmp.txt",2)
    ;$MountFile=FileOpen("D:\_Windows8\Make_WinPE\tmp.txt",2)
    FileWrite($MountFile,$tmp)
    $Result = FileClose($MountFile)
    RunWait("X:\Windows\System32\Diskpart.exe /s X:\Windows\System32\tmp.txt","X:\Windows\System32",@SW_HIDE)
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,356
    2,026
    210
    I do NOT boot to USB, I always boot via PXE, thanks
     
  5. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,964
    907
    60
    That doesn't make a difference. If you add FindUSB to your PXE booted WinPE, The USB will always be U:.
    Your title:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,356
    2,026
    210
    #6 sebus, Feb 23, 2017
    Last edited by a moderator: Apr 20, 2017
    (OP)
    NO, it will NOT:

    Code:
    If FileExists($arrDriveNames[2] & "\Boot\WinPE64.wim"
    I do NOT have %USB%\Boot\WinPE64.wim

    In fact the stick is always EMPTY!

    Again I solved it already, so no biggie, just wonder why original code does not work under PE
     
  7. bear_aussie

    bear_aussie MDL Senior Member

    Jun 8, 2015
    271
    292
    10
    #7 bear_aussie, Feb 24, 2017
    Last edited: Feb 26, 2017
    ok you've solved this but I thought id mention something...

    the lack of SETLOCAL ENABLEEXTENSIONS in your batch(es) caught my eye sebus

    compare-ops like EQU, GTR etc only work when command extensions are on

    I still run 7 so I dunno if win10 has made command extensions default

    if it has maybe 10PE doesn't?

    edit - your second script enables delayed expansion... which implicitly enables cmd extensions to my knowledge - that's why it works
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #8 Compo, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    Here's how to do it in a batch file
    Code:
    @Echo Off
    For /F "Skip=1" %%A In (
        '"WMIC LogicalDisk Where (DriveType='2') Get DeviceID" 2^>Nul'
    ) Do For %%B In (%%A) Do Echo=%%B is a USB drive
    Here's how to do it at the Command Prompt
    Code:
    X:\windows\system32>WMIC LogicalDisk Where (DriveType='2') Get DeviceID 2>Nul
    DeviceID
    D:
    In your Command Prompt line of code it should read:
    Code:
    X:\windows\system32>WMIC LogicalDisk Get Caption,Description,DriveType
    Caption  Description       DriveType
    C:       Local Fixed Disk  3
    D:       Removable Disk    2
    E:       CD-ROM Disc       5
    X:       Local Fixed Disk  3