Collecting System information using python

Shoumik Das 0 Reputation points
2024-05-07T18:28:27.4633333+00:00

How can I collect the information that System Information collects?

I want to view the USB port information within System Information in Python.

If possible, I would like to go to File and then save at the top left in python as well.

Any code snippets available?

System Center Orchestrator
System Center Orchestrator
A family of System Center products that provide an automation platform for orchestrating and integrating both Microsoft and non-Microsoft IT tools.
216 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XinGuo-MSFT 14,851 Reputation points
    2024-05-08T02:08:47.02+00:00

    Hi,

    If you want to get system information on a Windows machine using Python, you can use the pywin32 and wmi libraries. Here’s a simple example of how you can get USB information:

    Python

    import wmi
    
    # Initialize the WMI interface
    c = wmi.WMI()
    
    # Query for USB devices
    for usb in c.Win32_USBControllerDevice():
        print(usb.Dependent)
    

    This will print out a list of all USB devices connected to your system.

    To save this information to a file, you can modify the code like this:

    import wmi
    
    # Initialize the WMI interface
    c = wmi.WMI()
    
    # Open the output file
    with open('output.txt', 'w') as f:
        # Query for USB devices
        for usb in c.Win32_USBControllerDevice():
            print(usb.Dependent, file=f)
    
    

    This will write the output to a file named output.txt.

    Please note that you’ll need to install the pywin32 and wmi libraries if they’re not already installed. You can do this with pip:

    pip install pywin32 wmi
    

    Remember to run these scripts with appropriate permissions as accessing hardware information often requires administrator privileges. Also, please be aware that the exact information available may vary depending on the specific devices connected to your system and the version of the Windows operating system your system is running.

    I hope this helps! Let me know if you have any other questions. 😊

    1 person found this answer helpful.