3
1
My example code is below. I don't need drives like the cd rom and what not.
$drive = Get-WmiObject Win32_LogicalDisk -ComputerName $compName | ForEach-Object -Process {$_.DeviceID}
3
1
My example code is below. I don't need drives like the cd rom and what not.
$drive = Get-WmiObject Win32_LogicalDisk -ComputerName $compName | ForEach-Object -Process {$_.DeviceID}
2
Two methods using interface type or media type
First option interfacetype,
gwmi win32_diskdrive | ?{$_.interfacetype -eq "IDE" -or $_.interfacetype -eq "SCSI" }
Interface type of physical disk drive.
The values are:
Second option mediatype
gwmi win32_diskdrive | ?{$_.mediatype -eq "Fixed hard disk media"}
Possible values are:
to get phsyical drive
gwmi win32_diskdrive | ?{$_.mediatype -eq "Fixed hard disk media"} | % -Process {$_.DeviceID}
to get logical drive
gwmi win32_diskdrive | ?{$_.mediatype -eq "Fixed hard disk media"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}
1
If you want only physical drives it's as easy as
PS> Get-PhysicalDisk
FriendlyName SerialNumber MediaType CanPool OperationalStatus HealthStatus Usage Size
------------ ------------ --------- ------- ----------------- ------------ ----- ----
Generic USB SD Reader x Unspecified False OK Healthy Auto-Select 14.49 GB
Samsung SSD 840 PRO Series xxxxxxxxxxxxxxx SSD True OK Healthy Auto-Select ...47 GB
ST2000DL003-9VT166 xxxxxxxx HDD False OK Healthy Auto-Select 1.82 TB
ASMT 2105 xxxxxxxxxxxxxxxxxxxx Unspecified False OK Healthy Auto-Select ...51 GB
if you want the chain of associators this could help
$diskdrive = gwmi win32_diskdrive
foreach($drive in $diskdrive){
out-host -InputObject "`nDrive: deviceid-$($drive.deviceid.substring(4)) Model - $($drive.model)"
##partition
$partitions = gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($drive.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
foreach($part in $partitions){
Out-Host -InputObject "`tPartition: $($part.name)"
$vols = gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($part.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"
foreach($vol in $vols){
out-host -InputObject "`t`t$($vol.name)"
}
}
}
Sample Output
Drive: deviceid-PHYSICALDRIVE0 Model - ST2000DL003-9VT166
Partition: Disk #0, Partition #0
D:
Drive: deviceid-PHYSICALDRIVE1 Model - Samsung SSD 840 PRO Series
Partition: Disk #1, Partition #0
H:
Partition: Disk #1, Partition #1
C:
Partition: Disk #1, Partition #2
Drive: deviceid-PHYSICALDRIVE2 Model - Generic USB SD Reader USB Device
Partition: Disk #2, Partition #0
I:
Drive: deviceid-PHYSICALDRIVE3 Model - ASMT 2105 USB Device
Partition: Disk #3, Partition #0
You might find the output of
Get-WMIObject -Class Win32_LogicalDisk -Property * | Select-Object -First 1 | Get-Member
to be both interesting and useful. – Jeff Zeitlin – 2017-06-26T15:01:51.540if you have PowerShell v3+ and for local only, just use
get-physicaldisk
– SimonS – 2017-06-26T15:01:55.353