I would like to get the serial number, as in the serial number returned from the PowerShell Get-Disk cmdlet, associated with the drive letter of a windows volume. I'm strugling to find anything that will point me in the correct direction for how to do this. I can see that people have managed to do this on site this via WMI queries invoked from vbscript and c#, e.g.
private string GetDiskIndex(string driveLetter)
{
driveLetter = driveLetter.TrimEnd('\\');
ManagementScope scope = new ManagementScope(@"\root\cimv2");
var drives = new ManagementObjectSearcher(scope, new ObjectQuery("select * from Win32_DiskDrive")).Get();
foreach(var drive in drives)
{
var partitions = new ManagementObjectSearcher(scope, new ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")).Get();
foreach(var partition in partitions)
{
var logicalDisks = new ManagementObjectSearcher(scope, new ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition")).Get();
foreach (var logicalDisk in logicalDisks)
{
if (logicalDisk["DeviceId"].ToString() == driveLetter) return partition["DiskIndex"].ToString();
}
}
}
return null;
}
Is there an elegant way of doing this in PowerShell ?