在 PowerShell 中获取 U 盘的盘符 [英] Get the drive letter of USB drive in PowerShell
问题描述
我看过 C# 和其他一些语言的文章,解释了如何实现我正在寻找的东西,但我不知道如何转换它们.
I've seen articles in C# and some other languages that explain how to achieve what I'm looking for but I don't know how to convert them.
以下链接解释了如何获得答案:
如何获取 USB 的盘符设备?
Win32_DiskDrive->Win32_DiskDriveToDiskPartition ->Win32_DiskPartition ->Win32_LogicalDiskToPartition ->Win32_LogicalDisk
The following link explains how to get the answer:
How can I get the drive letter of an USB device?
Win32_DiskDrive-> Win32_DiskDriveToDiskPartition -> Win32_DiskPartition -> Win32_LogicalDiskToPartition -> Win32_LogicalDisk
GEOCHET 的回答也解释了如何获得答案,但同样不是在 PowerShell 中:如何找到U盘盘符?
The answer by GEOCHET explains also explains how to achieve the answer but again, not in PowerShell: How to find USB drive letter?
推荐答案
尝试:
gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{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}
已通过插入一个或多个 USB 设备进行测试.
Tested with one and more than one USB device plugged-in.
这篇关于在 PowerShell 中获取 U 盘的盘符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!