如何通过PowerShell获取媒体设备驱动路径?当前代码输出空白
解决PowerShell获取媒体设备驱动路径的问题
你的代码输出空白是因为Get-PnpDevice返回的对象并没有名为Driver的属性,要获取媒体设备的驱动路径,需要借助PowerShell的其他cmdlet或CIM类来查询相关属性。以下是几种可行的解决方案:
方法一:结合Get-PnpDevice和Get-PnpDeviceProperty
通过设备实例ID查询系统定义的驱动属性键,直接获取路径和版本:
$mediaDevices = Get-PnpDevice -Class MEDIA -Status OK foreach ($device in $mediaDevices) { # 获取驱动路径 $driverPath = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName DEVPKEY_Driver_DriverPath | Select-Object -ExpandProperty Data # 获取驱动版本 $driverVersion = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName DEVPKEY_Driver_Version | Select-Object -ExpandProperty Data Write-Host "设备名称: $($device.FriendlyName)" Write-Host "驱动路径: $driverPath" Write-Host "驱动版本: $driverVersion" Write-Host "-------------------------" }
-Status OK用于过滤掉未正常运行的设备,避免无效数据;DEVPKEY_Driver_DriverPath是系统内置的属性键,专门用于获取驱动文件的所在路径。
方法二:使用Get-WindowsDriver(需管理员权限)
这个cmdlet直接返回已安装驱动的完整信息,输出更简洁,但需要以管理员身份运行:
# 需右键以管理员身份启动PowerShell Get-WindowsDriver -Online -Class MEDIA | Select-Object DeviceName, DriverVersion, DriverPath
方法三:通过CIM类Win32_PnPSignedDriver查询
借助WMI的CIM类直接获取已签名驱动的详细数据,无需额外权限(部分系统环境可能需要):
Get-CimInstance Win32_PnPSignedDriver | Where-Object { $_.DeviceClass -eq 'MEDIA' } | Select-Object DeviceName, DriverVersion, DriverPath
内容的提问来源于stack exchange,提问作者YoshiW
相关产品推荐
相关产品推荐

