🐍 Python 3.10+

Export USB Controllers to CSV (Python)

Exports USB Controllers (Win32_USBController) to a timestamped CSV file via wmic.

By WindowsScripting.com · 1 KB
export_u_s_b_controller_to_csv.py
#!/usr/bin/env python3
"""
export_u_s_b_controller_to_csv.py

Exports USB Controllers (Win32_USBController) to a timestamped CSV file using the
built-in `wmic` command, stdlib only.

Usage:
    python export_u_s_b_controller_to_csv.py [output_folder]
"""

import subprocess
import sys
from datetime import datetime
from pathlib import Path


def main() -> None:
    out_folder = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".")
    out_folder.mkdir(parents=True, exist_ok=True)

    timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    out_path = out_folder / f"u_s_b_controller_{timestamp}.csv"

    result = subprocess.run(
        ["wmic", "path", "Win32_USBController", "get", "Name,Manufacturer,Status", "/format:csv"],
        capture_output=True, text=True, check=False,
    )

    if result.returncode != 0:
        sys.exit(f"wmic failed: {result.stderr.strip()}")

    out_path.write_text(result.stdout, encoding="utf-8")
    print(f"Exported USB Controllers to {out_path}")


if __name__ == "__main__":
    main()