Solved Alternative to wmic volume get driveLetter, label ?


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).
Interesting. Since I've got it coded your way I'll keep it even if it were less efficient. That code is trivial compared with the rest of the stuff the .bat files do.
 

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
while the other needs error handling...
For me, it doesn't. Both ways produce the same result and no PropertyNotFoundException ever occurs.
 

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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • 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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
Well I demonstrated that it does when a drive without the specified label exists. Maybe it doesn't in the batch file, that wasn't what I said.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
Well I demonstrated that it does when a drive without the specified label exists. Maybe it doesn't in the batch file, that wasn't what I said.
For me, It doesn't in the batch script, and it also doesn't in a PowerShell window. I used 3 different computers with completely different hardware to verify this. All are running on Windows 11 version 24H2 (OEM so, even if there are hidden issues at play, these issues could not have been carried over from one computer onto the others). But anyway. I like the simplicity of (Get-WmiObject...).DeviceID, and, the important thing IMO is that it works, which, at least for me, it does.
 

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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • 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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
I'm not sure what you're getting at, mate. If you specify a volume label that doesn't exist, you get back a null object, which of course does not have a DeviceID property, and you get an error when you try to reference it. "It works on my machine" is no comfort to anyone but the person who said it.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
I'm not sure what you're getting at, mate. If you specify a volume label that doesn't exist, you get back a null object, which of course does not have a DeviceID property
Correct.
, and you get an error when you try to reference it.
That's only because you have enabled strict mode in PowerShell. By default, strict mode is disabled. If you are using a PowerShell profile to enable strict mode, powershell -NoProfile will run PowerShell without loading the profile, and strict mode will be disabled in that PowerShell session as a result of this. This is why I specified the -NoProfile parameter in the batch script. The profile (if it exists, which, by default, it doesn't) is not needed for the intended purpose [what the OP described].
"It works on my machine" is no comfort to anyone but the person who said it.
It should work on every default, clean, installation of Windows 11. I did specify the -NoProfile parameter in the batch script so, it should work in the batch script as-is. (Even, if you have enabled strict mode.) If it turns out that it still doesn't, then you've got more serious problems to worry about.

That said, you can disable strict mode directly in PowerShell by running Set-StrictMode -Off if that's what you want. There is no Get-StrictMode Cmdlet, but you can use this function instead:
function Get-StrictMode { try { $null = ($null -eq @(1)[2]) } catch { return '3.0 or Latest' }; try { ''.Year } catch { return '2.0' }; try { $null = ($undefined -gt 1) } catch { return '1.0' }; return 'Off' }

@pokeefe0001
Sorry for TMI (Too Much Info).
 

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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • 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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
I guess you can make a bunch of assumptions and half-bake code, or you can do it correctly.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
I guess you can make a bunch of assumptions and half-bake code, or you can do it correctly.

I was going to just drop this and move on, but then I realized... this calls for a proper squad debrief.

Picture it: the trenches of PowerShell. The objective? Retrieve that elusive volume label without triggering strict mode landmines or getting bogged down in verbosity crossfire. My solution? Elegant. Efficient. Field-tested.

Enter Private ‘ExpandProperty’, declaring the high ground. And so I said—

“Back in ’85, I was entrusted with a divine gift—the power to wrangle memory-mapped editors and outsmart the TI video chip. Put me and this here For /F loop anywhere within one execution cycle of a mislabeled volume... war’s over, fellas. Disk mounted. Amen.”

Scripts go up, not down. Mine compiles to the interpreter. The interpreter compiles to the OS. The OS compiles to the legacy of every developer who ever dared to plug a Bitcorder into a Sony HB-75P.

Respect the process. Honor the context. And when in doubt, remember—not all battles are won with -ExpandProperty. Some are won with experience measured in kilobytes and ink ribbons.

 

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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • 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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
Hi all..
im trying to convert WMIC commands to powershell.... im after help on the following...

Echo *** Checking the type of computer
Echo.
set chassis=Nil
for /f "tokens=2 delims==" %%a in ('wmic systemenclosure get chassistypes /format:value') do set pctype=%%a

if %pctype% == {1} set chassis=Unknown
if %pctype% == {2} set chassis=Unknown

if %pctype% == {3} set chassis=Desktop
if %pctype% == {4} set chassis=Desktop
if %pctype% == {6} set chassis=Desktop
if %pctype% == {7} set chassis=Desktop
if %pctype% == {13} set chassis=Desktop
if %pctype% == {16} set chassis=Desktop
if %pctype% == {15} set chassis=Desktop
if %pctype% == {24} set chassis=Desktop

if %pctype% == {8} set chassis=Laptop
if %pctype% == {9} set chassis=Laptop
if %pctype% == {10} set chassis=Laptop
if %pctype% == {14} set chassis=Laptop
if %pctype% == {31} set chassis=Laptop

if %chassis% == Nil Echo Chassis Type not Found!! - Type reported is : %pctype% && color 4F && Pause && color 0F


Im having difficulties ..... im a very basic programmer

ive come up with this in CMD
Powershell -NoProfile -Command Get-CimInstance -ClassName "Win32_SystemEnclosure | Select-Object -ExpandProperty ChassisTypes" >> "%userprofile%\downloads\Chassietype.txt"

but I dont know how to use the SET command from a powershell command
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
While waiting to get working PS commands WMIC can be added to Windows 11:



Consider opening a new thread so that you can create the title and make the thread more visible.
 

My Computer

System One

  • OS
    Windows 10
    Computer type
    Laptop
    Manufacturer/Model
    HP
    CPU
    Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz
    Motherboard
    Product : 190A Version : KBC Version 94.56
    Memory
    16 GB Total: Manufacturer : Samsung MemoryType : DDR3 FormFactor : SODIMM Capacity : 8GB Speed : 1600
    Graphics Card(s)
    NVIDIA Quadro K3100M; Intel(R) HD Graphics 4600
    Sound Card
    IDT High Definition Audio CODEC; PNP Device ID HDAUDIO\FUNC_01&VEN_111D&DEV_76E0
    Hard Drives
    Model Hitachi HTS727575A9E364
    Antivirus
    Microsoft Defender
    Other Info
    Mobile Workstation
i found it!

For /F "delims=" %%G In ('PowerShell -Command "&Get-CimInstance -ClassName Win32_SystemEnclosure | Select-Object -ExpandProperty ChassisTypes"') Do Set pctype=%%G
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
i found it!

For /F "delims=" %%G In ('PowerShell -Command "&Get-CimInstance -ClassName Win32_SystemEnclosure | Select-Object -ExpandProperty ChassisTypes"') Do Set pctype=%%G
Here is an alternative solution:
Batch:
@echo off
echo *** Checking the type of computer
echo.
for /f "delims=" %%G in ('powershell -NoProfile -Command "$s = 'The Chassis Type is {0}.'; switch ((Get-WmiObject -ClassName Win32_SystemEnclosure -Property ChassisTypes).ChassisTypes[0]) { {$_ -in 1,2} {$s -f 'Unknown'}; {$_ -in 3,4,6,7,13,15,16,24} {$s -f 'Desktop'}; {$_ -in 8,9,10,14,31} {$s -f 'Laptop'}; default {'Chassis Type not Found!! - Type reported is : {0}. & color 4f' -f $_} }"') DO set V=%%G
echo %V%
pause
color 0f
 

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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • 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
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
I have another problem

@echo off

For /F "delims=" %%E In ('PowerShell -Command "&Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object -ExpandProperty DeviceID"') do (

echo %%E
set myVariable=%%E
echo Original variable: %myVariable%

set myVariable=%myVariable::=%
echo Variable after stripping colons: %myVariable%

)



it displays the drive letter at %%E, but when I try to strip the ":" it just seems to ignore!! at set myVariable=%%E
i want to strip the ":" off the C: so I can use the C in a folder name (because you cant use C: as a folder name)
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
You'll need to use delayed expansion...

Batch:
@echo off
setlocal enabledelayedexpansion

For /F "delims=" %%E In ('PowerShell -Command "& Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object -ExpandProperty DeviceID"') do (

    echo %%E

    set myVariable=%%E
    echo Original variable: !myVariable!

    set myVariable=!myVariable::=!
    echo Variable after stripping colons: !myVariable!

)

endlocal
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
You'll need to use delayed expansion...

Batch:
@echo off
setlocal enabledelayedexpansion

For /F "delims=" %%E In ('PowerShell -Command "& Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object -ExpandProperty DeviceID"') do (

    echo %%E

    set myVariable=%%E
    echo Original variable: !myVariable!

    set myVariable=!myVariable::=!
    echo Variable after stripping colons: !myVariable!

)

endlocal
thank you so much!! - I think i saw all of that "setlocal enabledelayedexpansion", but didnt relise I needed to have a "endlocal"
 
Last edited:

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
Is there a reason for using the "&" operator? It's not really needed for calling Get-CimInstance, and you can remove it.
 

My Computer

System One

  • OS
    Windows 7
Just to bother you, I think. ;-)
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
Is there a reason for using the "&" operator? It's not really needed for calling Get-CimInstance, and you can remove it.
no not really.... but I have removed..... its what I found using google...
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
AI tends to pick up bad programming style, without understanding why it's done. It doesn't hurt your code examples, but someone might get the wrong impression it's required on every PS one-liner.

& is used to make a call to an external program, or to call a script block (another mini-script inside your code). Get-CimInstance is an internal PS command, so it doesn't need the & to work.

For cleaner batch files, you can also omit the -Command argument when you feed PS an one-line script that's inside double quotes.

powershell "Get-CimInstance..." is the same as powershell -Command "Get-CimInstance..."
 

My Computer

System One

  • OS
    Windows 7
AI tends to pick up bad programming style, without understanding why it's done. It doesn't hurt your code examples, but someone might get the wrong impression it's required on every PS one-liner.

& is used to make a call to an external program, or to call a script block (another mini-script inside your code). Get-CimInstance is an internal PS command, so it doesn't need the & to work.

For cleaner batch files, you can also omit the -Command argument when you feed PS an one-line script that's inside double quotes.

powershell "Get-CimInstance..." is the same as powershell -Command "Get-CimInstance..."
thank you.... ive tried this and its working..... I do like to keep the code clean if I can
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop

Latest Support Threads

Back
Top Bottom