欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth\(R\)" } 只想得到两列 Status 和 FriendlyName AG chatgpt 有大用 有大大用

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 两列内容的命令:

powershell
Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth\(R\)" } | Select-Object Status, FriendlyName

执行这个命令后,输出将只包含这两列,如下所示:

Status FriendlyName
------ ------------
OK     英特尔(R) 无线 Bluetooth(R)

如果你想要更紧凑的表格格式,可以添加 -AutoSize 参数:

powershell
Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" } | Select-Object Status, FriendlyName | Format-Table -AutoSize



只想得到  Status   这一列

如果只想得到 Status 这一列的输出(去掉标题和其他列),可以使用以下命令之一:


方法 1:直接输出 Status 的值

powershell
(Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" }).Status

输出示例:

OK

(仅返回 <Status> 值,不显示列名)


方法 2:使用 Select-Object -ExpandProperty

powershell
Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" } | Select-Object -ExpandProperty Status

输出示例:

OK

(同样只输出 <Status> 的值)


方法 3:提取成变量(如果需要存储)

powershell
$btStatus = (Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" }).Status Write-Output $btStatus

用途
如果后续脚本需要使用 $btStatus(例如判断是否 OK),这种方法比较方便。


方法 4:简化格式化(仅 Format-Table -HideTableHeaders

powershell
Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth$R$" } | Select-Object Status | Format-Table -HideTableHeaders

输出示例:

OK

(只保留 Status 值,不显示表格格式)


总结

  • 如果只是查看 Status 值,推荐 方法 1 或 方法 2(最简单)。

  • 如果要用于脚本判断,推荐 方法 3(存储为变量)。

  • 如果希望表格简洁,可以用 方法 4

选择最适合你的方式即可! 🚀






普通分类: