Question:
I’m trying to get a list of drive letters in a drop-down menu. I’m currently using the code below and it works just fine in Windows 10, but doesn’t work at all in Windows 7.
1 2 3 |
$Drive_Letters = Get-WmiObject Win32_LogicalDisk ForEach ($Drives in $Drive_Letters.DeviceID) { $Dest_Drive_Box.Items.Add($Drives) } |
In Win 7 I tried adjusting the code to this…
1 2 3 |
$Drive_Letters = Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID ForEach ($Drives in $Drive_Letters) { $Dest_Drive_Box.Items.Add($Drives) } |
But now it shows “@DeviceID=C:}”, “@DeviceID=D:}”, etc. in Win 7 and 10 for each drive letter. I need to just show “C:”, “D:”, etc.
Thanks!
Answer:
1 2 |
Get-PSDrive |
This will return all drives mapped in the current session. The
Name
property contains the drive letter.
To capture just drive letters:
1 2 3 4 5 6 |
(Get-PSDrive).Name -match '^[a-z] Tested working in PSv2:
|
Source:
How to get list of drive letters in Powershell 2.0 by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
Tested working in PSv2:
1 |
Source:
How to get list of drive letters in Powershell 2.0 by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
Source:
How to get list of drive letters in Powershell 2.0 by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
Tested working in PSv2:
1 |