- 1 year ago
Powershell: Extract physical disk properties based on a logical drive name or a filepath
To extract physical disk properties based on a logical drive name or a file path using PowerShell, you can use the Get-WmiObject
cmdlet along with the Win32_LogicalDisk
and Win32_DiskDrive
classes. Here's an example script that demonstrates this:
powershell# Function to get physical disk information based on logical drive name or file path
function Get-PhysicalDiskProperties {
param (
[Parameter(Position = 0, Mandatory = $true)]
[string]$Path
)
# Get the logical disk object
$logicalDisk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='$((Get-Item -Path $Path).PSDrive.Name)'"
if ($logicalDisk) {
# Get the physical disk object based on the disk ID
$diskDrive = Get-WmiObject -Class Win32_DiskDrive -Filter "DeviceID='$($logicalDisk.PNPDeviceID -replace '\\', '\\')'"
if ($diskDrive) {
# Output the physical disk properties
$diskDrive | Select-Object DeviceID, Model, Manufacturer, InterfaceType, MediaType, Size
}
else {
Write-Host "No physical disk found for logical drive '$($logicalDisk.DeviceID)'"
}
}
else {
Write-Host "Logical drive not found for path '$Path'"
}
}
# Usage example
$filePath = "C:\path\to\file.txt"
Get-PhysicalDiskProperties -Path $filePath
In this script, the Get-PhysicalDiskProperties
function takes a Path
parameter, which can be a logical drive name or a file path. It retrieves the corresponding logical disk object using Get-WmiObject
with the Win32_LogicalDisk
class. It then extracts the PNPDeviceID
property from the logical disk object and uses it to find the associated physical disk object using Get-WmiObject
with the Win32_DiskDrive
class.
Finally, it outputs the desired physical disk properties, such as DeviceID
, Model
, Manufacturer
, InterfaceType
, MediaType
, and Size
.
You can modify the script as needed to include additional properties or customize the output format.