欢迎各位兄弟 发布技术文章
这里的技术是共享的
PS C:\WINDOWS\system32> Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth\(R\)" }
Status Class FriendlyName InstanceId
------ ----- ------------ ----------
OK Bluetooth 英特尔(R) 无线 Bluetooth(R) USB\VID_8087...
只想得到 Status 和 FriendlyName 两列内容
你可以使用 Select-Object cmdlet 来选择特定的属性列。以下是只显示 Status 和 FriendlyName 两列内容的命令:
powershellGet-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth\(R\)" } | Select-Object Status, FriendlyName
执行这个命令后,输出将只包含这两列,如下所示:
Status FriendlyName
------ ------------
OK 英特尔(R) 无线 Bluetooth(R)
如果你想要更紧凑的表格格式,可以添加 -AutoSize 参数:
powershellGet-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" } | Select-Object Status, FriendlyName | Format-Table -AutoSize
只想得到 Status 这一列
如果只想得到 Status 这一列的输出(去掉标题和其他列),可以使用以下命令之一:
Status 的值powershell(Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" }).Status
输出示例:
OK
(仅返回 <Status> 值,不显示列名)
Select-Object -ExpandPropertypowershellGet-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" } | Select-Object -ExpandProperty Status
输出示例:
OK
(同样只输出 <Status> 的值)
powershell$btStatus = (Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" }).Status Write-Output $btStatus
用途
如果后续脚本需要使用 $btStatus(例如判断是否 OK),这种方法比较方便。
Format-Table -HideTableHeaders)powershellGet-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" } | Select-Object Status | Format-Table -HideTableHeaders
输出示例:
OK
(只保留 Status 值,不显示表格格式)
如果只是查看 Status 值,推荐 方法 1 或 方法 2(最简单)。
如果要用于脚本判断,推荐 方法 3(存储为变量)。
如果希望表格简洁,可以用 方法 4。
选择最适合你的方式即可! 🚀