本文目录导读:

Windows (Powershell)
在 Windows 上,Get-PnpDevice 是最强大的命令,它可以列出所有即插即用设备(包括键盘、鼠标、U盘、显卡等)。
脚本 (PowerShell):
# 获取所有连接的外设
Get-PnpDevice -PresentOnly | Where-Object { $_.Class -ne "System" -and $_.Class -ne "Processor" } | Select-Object Status, Class, FriendlyName, InstanceId
# 更详细的:只获取可用的(非错误)外设
Get-PnpDevice -PresentOnly | Where-Object { $_.Status -eq "OK" -and $_.Class -ne "System" } | Format-Table -AutoSize
# 获取 USB 设备列表
Get-PnpDevice -PresentOnly | Where-Object { $_.Class -eq "USB" } | Select-Object FriendlyName, Class, Status
# 获取所有磁盘驱动器(包括 U盘、外接硬盘)
Get-PnpDevice -PresentOnly | Where-Object { $_.Class -eq "DiskDrive" } | Select-Object FriendlyName, Size, Status
如何运行:
- 打开 PowerShell (管理员权限不是必须的,但能获取更多信息)。
- 粘贴上述命令中的任意一行,按回车。
Windows (CMD / Batch)
CMD 本身功能有限,但可以利用 wmic 或 pnputil 或 devcon。
方法 A: 使用 wmic (WMI 命令行)
@echo off :: 获取所有外设(类型) wmic path Win32_PnPEntity where "ConfigManagerErrorCode = 0" get DeviceID, Description, Manufacturer, Status :: 只获取 USB 设备 wmic path Win32_USBControllerDevice get Dependent :: 获取磁盘驱动器 wmic path Win32_DiskDrive get Model, InterfaceType, Size
方法 B: 使用 pnputil
:: 列出所有设备(需要管理员权限否,取决于系统版本) pnputil /enum-devices /connected :: 保存到文件 pnputil /enum-devices /connected > peripherals.txt type peripherals.txt
如何运行:
- 将代码保存为
list_peripherals.bat。 - 以管理员身份运行。
Linux (Bash)
Linux 下的信息分散在 /sys, /proc, udevadm, 和 lsusb 等命令中。
脚本 (Bash):
#!/bin/bash
echo "=== USB 设备列表 ==="
lsusb
echo ""
echo "=== 输入设备 (鼠标、键盘、触摸板) ==="
ls -l /dev/input/by-id/ | grep -v "event"
echo ""
echo "=== 所有挂载的存储设备 ==="
lsblk -o NAME,TRAN,SIZE,MODEL,MOUNTPOINT
echo ""
echo "=== 系统外设详细信息 (udevadm) ==="
# 列出所有已连接的 USB 设备路径(只看子设备,避免重复)
for device in /sys/bus/usb/devices/*/product; do
if [ -f "$device" ]; then
echo "USB Device: $(cat $device)"
fi
done
# 或者更简单
# 使用 hwinfo(需要安装:sudo apt install hwinfo)
# hwinfo --short | grep -i "usb\|disk\|mouse\|keyboard"
如何运行:
- 将代码保存为
list_peripherals.sh。 - 赋予执行权限:
chmod +x list_peripherals.sh - 运行:
./list_peripherals.sh
macOS (Bash / Zsh)
macOS 基于 BSD,使用 system_profiler 和 ioreg。
脚本 (Zsh/Bash):
#!/bin/zsh echo "=== USB 设备 ===" system_profiler SPUSBDataType | grep -E "Product:|Manufacturer:|Speed:|Serial Number" echo "" echo "=== 所有存储设备 (包括外接磁盘) ===" system_profiler SPStorageDataType | grep -A 10 "External" | grep -E "Name:|Volume Type:|Writable:|File System:" echo "" echo "=== 蓝牙设备 ===" system_profiler SPBluetoothDataType | grep -E "Name:|Address:|Services:|Connected:" echo "" echo "=== 接口 (USB, Thunderbolt, Display) ===" system_profiler SPDisplaysDataType 2>/dev/null | grep -E "Display Type:|Resolution:|Connection Type:" # 如果安装了 `brew install usbutils`,也可以使用 `lsusb` # lsusb
如何运行:
- 将代码保存为
list_peripherals_mac.sh。 - 赋予执行权限:
chmod +x list_peripherals_mac.sh - 运行:
./list_peripherals_mac.sh
通用跨平台脚本 (Python)
如果你希望一个脚本在多个操作系统上运行,或者进行更复杂的数据处理(如 JSON 输出),Python 是一个好选择。
Python 版本:
import platform
import subprocess
import json
def get_windows_devices():
result = subprocess.run(['powershell', '-Command',
'Get-PnpDevice -PresentOnly | Where-Object { $_.Status -eq "OK" } | Select-Object FriendlyName, Class, InstanceId | ConvertTo-Json'],
capture_output=True, text=True)
return json.loads(result.stdout)
def get_linux_devices():
devices = []
# 使用 lsusb
result = subprocess.run(['lsusb'], capture_output=True, text=True)
for line in result.stdout.strip().split('\n'):
if line:
devices.append({"type": "USB", "description": line})
# 如果有 hwinfo 更好
try:
result = subprocess.run(['hwinfo', '--short'], capture_output=True, text=True)
if result.returncode == 0:
for line in result.stdout.strip().split('\n'):
devices.append({"type": "hwinfo", "description": line})
except FileNotFoundError:
pass
return devices
def get_mac_devices():
result = subprocess.run(['system_profiler', 'SPUSBDataType', '-json'],
capture_output=True, text=True)
return json.loads(result.stdout)
# 主程序
system = platform.system()
if system == "Windows":
devices = get_windows_devices()
elif system == "Linux":
devices = get_linux_devices()
elif system == "Darwin": # macOS
devices = get_mac_devices()
else:
devices = []
print(json.dumps(devices, indent=2, ensure_ascii=False))
如何运行:
- 确保安装了 Python 3。
- 将代码保存为
list_peripherals.py。 - 运行:
python3 list_peripherals.py
| 操作系统 | 推荐方法 | 特点 |
|---|---|---|
| Windows | PowerShell Get-PnpDevice |
最详细,输出结构清晰,支持所有设备类型 |
| Windows | pnputil /enum-devices |
命令简单,适合 Batch 脚本 |
| Linux | lsusb + lsblk + ls -l /dev/input/ |
组合命令覆盖 USB、存储、输入设备 |
| Linux | udevadm info --export-db |
获得最原始的系统设备信息 |
| macOS | system_profiler SPUSBDataType |
内置,无需安装额外工具,输出美观 |
| 跨平台 | Python + subprocess | 统一输出格式(JSON),方便后续处理 |
如果你只需要快速看一眼,Windows 推荐 Get-PnpDevice | Where-Object { $_.Status -eq "OK" } | Format-Table -AutoSize,Linux 推荐 lsusb 配合 lsblk。