Solved Alternative to wmic volume get driveLetter, label ?


pokeefe0001

Well-known member
Member
VIP
Local time
4:08 PM
Posts
271
Location
Pacific Northwest USA
OS
Windows 11
I just upgraded to 24H2 and discovered that WMIC has been removed from Windows. I now have a bunch of dead maintenance batch files. I've re-added WMIC "Feature on Demand" to one computer but that's a temporary fix. Microsoft recommends converting to Powershell but I don't know PS and am likely to make matters worse. I might be able to convert the one subroutine bat file that contains the WMIC if the other .bat files can invoke a PS script and if PS can return a "global" variable that can be picked up by a .bat file. I would rather find a function that can be used in the .bat subroutine.

The subroutine looks for all drives with labels starting with a specific string. If there is only one, it returns the drive letter in a global variable. If there is none or more than one the script complains until the user makes sure there is only one.

The chunk of code (not the entire script) containing the WMIC command is
Code:
@echo off
SETLOCAL
:Test
:: Find a drive with label containing "SBD MM #"
:: Ignore the drive containing this script

Set LabelMask=SBD MM #

Set Drive=
Set Extra=
Set Here=%~d0%

for /f %%D in ('wmic volume get DriveLetter^, Label ^| find /I "%LabelMask%" ^| find /V "%Here%"') Do (
   if defined Drive set Extra=%%D
   if not defined Drive set Drive=%%D
   )
If "%Drive%"=="" GoTo NoFlash
If not "%Extra%"=="" GoTo DupDrive
GoTo Return

Any recommendations for a supported alternative?
 
Windows Build/Version
Win11 Pro build 26100.4652

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Microsoft
    CPU
    Intel Core i5-8400
    Motherboard
    ASUS PRIME H370-PLUS
    Memory
    16GB
    Graphics Card(s)
    Intel UHD Graphics 630
    Sound Card
    On board
    Monitor(s) Displays
    Samsung SyncMaster 2043BWX
    Screen Resolution
    1680 x 1050
    Hard Drives
    Samsung SSD 850 256GB
    WDC 1TB NVMe
    WD 3TB external USB drive
    PSU
    I don't remember
    Case
    Corsair something-or-other
    Cooling
    Air CPU + 2 case fans
    Keyboard
    DAS S Pro (Cherry Brown)
    Mouse
    Logitech USB of some sort
This should get you started...

Batch:
@echo off
setlocal

REM Set your target volume label here
set "TargetLabel=SBD"

REM call PowerShell to find the drive letter
for /f "delims=" %%D in ('powershell -nologo -command ^
    "Get-Volume | Where-Object { $_.FileSystemLabel -like '*%TargetLabel%*' } | Select-Object -ExpandProperty DriveLetter"') do (
    set "DriveLetter=%%D:"
)

REM Check result
if defined DriveLetter (
    echo Found drive %DriveLetter% with label %TargetLabel%
) else (
    echo Drive with label "%TargetLabel%" not found.
)

endlocal
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
Thank you. I'll check that out. It occurred to me after posting my question that there must be a way to invoke a PS cmdlet from a .bat script and you confirmed that ... and provided me with the cmdlet.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Microsoft
    CPU
    Intel Core i5-8400
    Motherboard
    ASUS PRIME H370-PLUS
    Memory
    16GB
    Graphics Card(s)
    Intel UHD Graphics 630
    Sound Card
    On board
    Monitor(s) Displays
    Samsung SyncMaster 2043BWX
    Screen Resolution
    1680 x 1050
    Hard Drives
    Samsung SSD 850 256GB
    WDC 1TB NVMe
    WD 3TB external USB drive
    PSU
    I don't remember
    Case
    Corsair something-or-other
    Cooling
    Air CPU + 2 case fans
    Keyboard
    DAS S Pro (Cherry Brown)
    Mouse
    Logitech USB of some sort
PS supports both Get-WmiObject and Get-CimInstance calls to mimic a WMIC command line, but often there's a built-in command for the more common management tasks. Sometimes you will have time to learn the native PS methods, but other times you just want to copy a WMIC-like expression.
 

My Computer

System One

  • OS
    Windows 7
Yep, but ooo man I do not like to think about querying WMI, via PowerShell, from cmd. You're going to be caretting and backslashing and otherwise escaping until the cows come home.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
Google AI

@echo off
powershell.exe -Command "Get-CimInstance -ClassName Win32_LogicalDisk | Format-List DeviceID,VolumeName,Size,FreeSpace" > logical_disks.txt
echo Output saved to logical_disks.txt
pause

for network storage devices

$nd = $(Get-CIMinstance Win32_MappedLogicalDisk)
 

My Computer

System One

  • OS
    Microsoft Windows 11 Home
    Computer type
    PC/Desktop
    Manufacturer/Model
    MSI MS-7D98
    CPU
    Intel Core i5-13490F
    Motherboard
    MSI B760 GAMING PLUS WIFI
    Memory
    2 x 16 Patriot Memory (PDP Systems) PSD516G560081
    Graphics Card(s)
    GIGABYTE GeForce RTX 4070 WINDFORCE OC 12G (GV-N4070WF3OC-12GD)
    Sound Card
    Bluetooth Аудио
    Monitor(s) Displays
    INNOCN 15K1F
    Screen Resolution
    1920 x 1080
    Hard Drives
    WD_BLACK SN770 250GB
    KINGSTON SNV2S1000G (ELFK0S.6)
    PSU
    Thermaltake Toughpower GF3 1000W
    Case
    CG560 - DeepCool
    Cooling
    ID-COOLING SE-224-XTS / 2 x 140Mm Fan - rear and top; 3 x 120Mm - front
    Keyboard
    Corsair K70 RGB TKL
    Mouse
    Corsair KATAR PRO XT
    Internet Speed
    100 Mbps
    Browser
    Firefox
    Antivirus
    Microsoft Defender Antivirus
    Other Info
    https://www.userbenchmark.com/UserRun/66553205
Google would get a failing grade in scripting class.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
Format-Table would be less annoying, since each drive is one line of results.
You know... because CMD's built-in parsing abilities are @$$.
 

My Computer

System One

  • OS
    Windows 7
Batch:
@echo off
SETLOCAL
:Test
:: Find a drive with label containing "SBD MM #"
:: Ignore the drive containing this script

Set LabelMask=SBD MM #

Set Drive=
Set Extra=
Set Here=%~d0%

for /f "usebackq" %%D in (`"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command ^(Get-Volume^^^|Where-Object{$_.FileSystemLabel.StartsWith^('%LabelMask%'^,[System.StringComparison]::OrdinalIgnoreCase^) -and $_.DriveLetter -ne '%Here:~0,1%'}^).DriveLetter`)do (
   if defined Drive set Extra=%%D
   if not defined Drive set Drive=%%D
)
If "%Drive%"=="" GoTo NoFlash
If not "%Extra%"=="" GoTo DupDrive
GoTo Return
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
I tried both "Get-Volume" and "Get-CimInstance -ClassName Win32_LogicalDisk". For some reason Get-Volume introduces about a 6 second delay where Get-CimInstance was nearly instantaneous so I'm going with that.

Thank you.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Microsoft
    CPU
    Intel Core i5-8400
    Motherboard
    ASUS PRIME H370-PLUS
    Memory
    16GB
    Graphics Card(s)
    Intel UHD Graphics 630
    Sound Card
    On board
    Monitor(s) Displays
    Samsung SyncMaster 2043BWX
    Screen Resolution
    1680 x 1050
    Hard Drives
    Samsung SSD 850 256GB
    WDC 1TB NVMe
    WD 3TB external USB drive
    PSU
    I don't remember
    Case
    Corsair something-or-other
    Cooling
    Air CPU + 2 case fans
    Keyboard
    DAS S Pro (Cherry Brown)
    Mouse
    Logitech USB of some sort
Batch:
@echo off
SETLOCAL
:Test
:: Find a drive with label containing "SBD MM #"
:: Ignore the drive containing this script

Set LabelMask=SBD MM #

Set Drive=
Set Extra=
Set Here=%~d0%

for /f "usebackq" %%D in (`"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command ^(Get-WmiObject -ClassName Win32_LogicalDisk^^^|Where-Object{$_.VolumeName.StartsWith^('%LabelMask%'^,[System.StringComparison]::OrdinalIgnoreCase^) -and $_.DeviceID -ne '%Here%'}^).DeviceID`)do (
   if defined Drive set Extra=%%D
   if not defined Drive set Drive=%%D
)
If "%Drive%"=="" GoTo NoFlash
If not "%Extra%"=="" GoTo DupDrive
GoTo Return
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
I tried both "Get-Volume" and "Get-CimInstance -ClassName Win32_LogicalDisk". For some reason Get-Volume introduces about a 6 second delay where Get-CimInstance was nearly instantaneous so I'm going with that.

Thank you.
Right. Get-Volume is computing additional columns. For example, the HealthStatus and OperationalStatus properties returned by Get-Volume are not part of Win32_Volume.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
Batch:
@echo off
SETLOCAL
:Test
:: Find a drive with label containing "SBD MM #"
:: Ignore the drive containing this script

Set LabelMask=SBD MM #

Set Drive=
Set Extra=
Set Here=%~d0%

for /f "usebackq" %%D in (`"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command ^(Get-WmiObject -ClassName Win32_LogicalDisk^^^|Where-Object{$_.VolumeName.StartsWith^('%LabelMask%'^,[System.StringComparison]::OrdinalIgnoreCase^) -and $_.DeviceID -ne '%Here%'}^).DeviceID`)do (
   if defined Drive set Extra=%%D
   if not defined Drive set Drive=%%D
)
If "%Drive%"=="" GoTo NoFlash
If not "%Extra%"=="" GoTo DupDrive
GoTo Return
You've twice posted your example with no explanation. Since I do not know PS at all I have some questions and comments.
  • I do understand the need for "ne %Here%" test. I need to include it.
  • I don't understand the need for "System.StringComparison]::OrdinalIgnoreCase". The device labels in question will always be upper case.
  • I don't understand the need for "%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" vs just powershell. Isn't the Powershell path standard in the path variable?
  • I assume the caret is used for continuation across a line break. Is there some special use of "^^^" or did you have multiple blank lines in your original script?
I'm incorporating some of this into my script.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Microsoft
    CPU
    Intel Core i5-8400
    Motherboard
    ASUS PRIME H370-PLUS
    Memory
    16GB
    Graphics Card(s)
    Intel UHD Graphics 630
    Sound Card
    On board
    Monitor(s) Displays
    Samsung SyncMaster 2043BWX
    Screen Resolution
    1680 x 1050
    Hard Drives
    Samsung SSD 850 256GB
    WDC 1TB NVMe
    WD 3TB external USB drive
    PSU
    I don't remember
    Case
    Corsair something-or-other
    Cooling
    Air CPU + 2 case fans
    Keyboard
    DAS S Pro (Cherry Brown)
    Mouse
    Logitech USB of some sort
The StringComparison value is specified because, by default, StartsWith does a case-sensitive (and culture-sensitive) string comparison. You can ignore it if you know the values will all be uppercase, but I can tell you this is one of those things that will bite you some Friday afternoon when you're just trying to go home for the day.

PowerShell.exe is usually in the path; it's actually usually pretty close to the front in the path.

All that caretting and backticking and such is what I mentioned in post 5. It's because of the difference in special character handling in cmd vs PowerShell.

Edit: Just for fun, here's my original batch, rewritten to use CIM/WMI.

Batch:
@echo off
setlocal

REM Set your target volume label here
set TargetLabel=Banana
set Here=%~d0

REM call PowerShell to find the drive letter
for /f "delims=" %%D in ('powershell -nologo -command ^
    "Get-CimInstance -ClassName 'Win32_LogicalDisk' | Where-Object { ($_.VolumeName -like '*%TargetLabel%*') -and ($_.DeviceID -ne '%Here%') } | Select-Object -ExpandProperty DeviceID"') do (
    set "DriveLetter=%%D"
)

REM Check result
if defined DriveLetter (
    echo Found drive %DriveLetter% with label %TargetLabel%
) else (
    echo Drive with label "%TargetLabel%" not found.
)

endlocal
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
The StringComparison value is specified because, by default, StartsWith does a case-sensitive (and culture-sensitive) string comparison. You can ignore it if you know the values will all be uppercase, but I can tell you this is one of those things that will bite you some Friday afternoon when you're just trying to go home for the day.

PowerShell.exe is usually in the path; it's actually usually pretty close to the front in the path.

All that caretting and backticking and such is what I mentioned in post 5. It's because of the difference in special character handling in cmd vs PowerShell.
It looks like I can get by with -like by just removing the leading * in the comparison string.

I'll assume for now that PowerShell will be in the path for every computer I need to worry about. (Luckily, that's very few.) That will avoid the need for backticking, etc.

The caretting I've (sort of) figured our by trial and error (which seems to be my technique whenever dealing with an unfamiliar scripting language). I think I've got it under control for now.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Microsoft
    CPU
    Intel Core i5-8400
    Motherboard
    ASUS PRIME H370-PLUS
    Memory
    16GB
    Graphics Card(s)
    Intel UHD Graphics 630
    Sound Card
    On board
    Monitor(s) Displays
    Samsung SyncMaster 2043BWX
    Screen Resolution
    1680 x 1050
    Hard Drives
    Samsung SSD 850 256GB
    WDC 1TB NVMe
    WD 3TB external USB drive
    PSU
    I don't remember
    Case
    Corsair something-or-other
    Cooling
    Air CPU + 2 case fans
    Keyboard
    DAS S Pro (Cherry Brown)
    Mouse
    Logitech USB of some sort
You've twice posted your example with no explanation.
No, the 1st one uses Get-Volume. Get-CimInstance is a wrapper for Get-WmiObject if you're running on Windows.
Since I do not know PS at all I have some questions and comments.
  • I do understand the need for "ne %Here%" test. I need to include it.
  • I don't understand the need for "System.StringComparison]::OrdinalIgnoreCase". The device labels in question will always be upper case.
Your original script uses find /I. The /I (or /i) parameter specifies that the search is not case-sensitive.
Specifying [System.StringComparison]::OrdinalIgnoreCase as the second parameter in the StartsWith() method call also makes this method call not case-sensitive. To make it case-sensitive, just replace OrdinalIgnoreCase with Ordinal.

As an alternative, you could also decide to replace [System.StringComparison]::OrdinalIgnoreCase with 5 or replace [System.StringComparison]::Ordinal with 4—depending what it is that you want to achieve. StringComparison Enum (System)
  • I don't understand the need for "%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" vs just powershell. Isn't the Powershell path standard in the path variable?
While it is true that specifying the filepath isn't a strict necessity, for some, running just powershell.exe was causing the wrong version of PowerShell to run such a way that it ended up breaking the command even though the command itself was correct if running in PowerShell 5.1, and took a while to figure that out). If it works for you without the path, then you could always decide to remove the path, BUT... remember that if you do that, you might still pitfall at some point in the future, and, IMO there is no need to take this risk.
  • I assume the caret is used for continuation across a line break. Is there some special use of "^^^" or did you have multiple blank lines in your original script?
Although it's possible to use the caret for continuation across a line break, in the script I gave you, I only use the caret to escape special characters. The pipe symbol is doubly escaped because the for /f command runs another cmd session under the hood (to be able to run powershell.exe) so:
  1. ^^ becomes ^ and ^| becomes | giving you ^| which
  2. then becomes |.
I'm incorporating some of this into my script.
If you have any further questions/doubts about the command I wrote, just ask.
 
Last edited:

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
You could also do like this:
for /f %%D in ('^^^"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe^^^" -NoProfile -ExecutionPolicy Bypass -Command ^ "(Get-WmiObject -ClassName Win32_LogicalDisk|Where-Object{$_.VolumeName -like '%LabelMask%*' -and $_.DeviceID -ne '%Here%'}).DeviceID"')do (

...or this:
for /f %%D in ('powershell -NoProfile -ExecutionPolicy Bypass -Command ^ "(Get-WmiObject -ClassName Win32_LogicalDisk|Where-Object{$_.VolumeName -like '%LabelMask%*' -and $_.DeviceID -ne '%Here%'}).DeviceID"')do (
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
I know next to nothing about object oriented programming (a bit of an obstacle when it comes to PowerShell) so I'm unsure of what I'm seeing. Pseymour's technique uses "| Select-Object -ExpandProperty DeviceId" where you have you have ".DeviceID". Both extract the DeviceId property from the Win32_LogicalDisk object? I assume yours is slightly more efficient since the whole object gets piped to Select-Object in that technique. Other than that, is there a preference of one over the other?
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Microsoft
    CPU
    Intel Core i5-8400
    Motherboard
    ASUS PRIME H370-PLUS
    Memory
    16GB
    Graphics Card(s)
    Intel UHD Graphics 630
    Sound Card
    On board
    Monitor(s) Displays
    Samsung SyncMaster 2043BWX
    Screen Resolution
    1680 x 1050
    Hard Drives
    Samsung SSD 850 256GB
    WDC 1TB NVMe
    WD 3TB external USB drive
    PSU
    I don't remember
    Case
    Corsair something-or-other
    Cooling
    Air CPU + 2 case fans
    Keyboard
    DAS S Pro (Cherry Brown)
    Mouse
    Logitech USB of some sort
One way gracefully handles failure...

Powershell:
Get-CimInstance -ClassName 'Win32_LogicalDisk' | Where-Object { $_.VolumeName -eq 'Windows' } | Select-Object -ExpandProperty 'DeviceID'

C:

Get-CimInstance -ClassName 'Win32_LogicalDisk' | Where-Object { $_.VolumeName -eq 'Banana' } | Select-Object -ExpandProperty 'DeviceID'

while the other needs error handling...

Powershell:
(Get-CimInstance -ClassName 'Win32_LogicalDisk' | Where-Object { $_.VolumeName -eq 'Windows' }).DeviceId

C:

(Get-CimInstance -ClassName 'Win32_LogicalDisk' | Where-Object { $_.VolumeName -eq 'Banana' }).DeviceId

PropertyNotFoundException: The property 'DeviceId' cannot be found on this object. Verify that the property exists.

The entire object is piped out of the Where-Object either way.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
I ran each 1,000 times. The (Get-CimInstance...).DeviceID method ran an average of 6.1583 ms (range : 4.8111 to 117.9035 ms). The Select-Object method ran an average of 5.7888 ms (range : 4.8333 to 20.7353 ms).
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender

Latest Support Threads

Back
Top Bottom