{"id":4324,"library":"wmi","title":"Windows Management Instrumentation (WMI) Python Library","description":"The Python WMI module is a lightweight wrapper on top of the `pywin32` extensions, providing a more Python-friendly interface to Microsoft's Windows Management Instrumentation (WMI) API. It allows Python to query and manage various aspects of Windows systems, both locally and remotely, by abstracting the complexities of COM and WMI scripting. The latest version is 1.5.1, released in April 2020. The library is in maintenance mode with infrequent updates, as newer alternatives like `PyMI` and `windows-tools.wmi_queries` exist.","status":"maintenance","version":"1.5.1","language":"en","source_language":"en","source_url":"https://github.com/tjguk/wmi","tags":["windows","wmi","system-management","automation","pywin32"],"install":[{"cmd":"pip install wmi","lang":"bash","label":"Install wmi"}],"dependencies":[{"reason":"The `wmi` library acts as a lightweight wrapper around the `pywin32` extensions to interact with the underlying COM and WMI APIs on Windows. It is a mandatory prerequisite.","package":"pywin32","optional":false}],"imports":[{"note":"The primary class for connecting to the WMI service and performing queries.","symbol":"WMI","correct":"import wmi\nc = wmi.WMI()"}],"quickstart":{"code":"import wmi\nimport os\n\n# Connect to the local machine WMI service\n# For remote connection, set WMI_USERNAME and WMI_PASSWORD environment variables:\n# c = wmi.WMI(computer=\"remote_machine_ip\", user=os.environ.get('WMI_USERNAME', ''), password=os.environ.get('WMI_PASSWORD', ''))\n\ntry:\n    c = wmi.WMI()\n\n    # Get information about the operating system\n    for os_info in c.Win32_OperatingSystem():\n        print(f\"OS Name: {os_info.Caption}\")\n        print(f\"Version: {os_info.Version}\")\n        print(f\"Service Pack: {os_info.CSDVersion}\")\n        print(f\"Architecture: {os_info.OSArchitecture}\")\n        print(f\"Free Physical Memory: {int(os_info.FreePhysicalMemory) / (1024**2):.2f} GB\")\n\n    print(\"\\n--- Listing Running Processes (top 5) ---\")\n    # Get information about running processes (top 5)\n    for process in c.Win32_Process()[:5]:\n        print(f\"Process: {process.Name}, ID: {process.ProcessId}, Status: {process.Status}\")\n\n    print(\"\\n--- Listing Stopped Services (Auto Start) ---\")\n    # Find and list automatically starting services that are currently stopped\n    for s in c.Win32_Service(StartMode=\"Auto\", State=\"Stopped\"):\n        print(f\"Service: {s.Caption}, State: {s.State}\")\n\nexcept wmi.x_wmi as e:\n    print(f\"WMI Error: {e}\")\n    if \"Access is denied\" in str(e):\n        print(\"Ensure the script is run with administrator privileges, or correct remote authentication.\")\n    elif \"RPC server is unavailable\" in str(e):\n        print(\"Ensure the remote machine is reachable and the WMI service is running.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to connect to the local WMI service, retrieve basic operating system information, list a few running processes, and find automatically starting services that are currently stopped. It includes a placeholder for remote connections, emphasizing the use of environment variables for credentials for security best practice, and basic error handling for common WMI issues."},"warnings":[{"fix":"Only use this library on Windows operating systems. For cross-platform WMI-like communication (from non-Windows to Windows), consider tools like `py-wmi-client` that implement client-side WMI protocols.","message":"The `wmi` library is strictly Windows-specific and will not function on Linux or macOS. It directly interacts with the Windows Management Instrumentation service and depends on the `pywin32` extensions, which are only available for Windows environments.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Run your Python script with administrator privileges. For remote connections, ensure the `user` and `password` parameters are correct, the WMI service is running on the target, and Windows Firewall allows WMI traffic (e.g., enable 'Remote Administration' firewall rule). DCOM configuration might also need adjustment for specific scenarios.","message":"Accessing WMI data, especially for system-level information or remote machines, frequently requires elevated privileges. Common errors like 'Access Denied' or 'RPC Server Unavailable' indicate insufficient permissions or network/firewall blocks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For performance-critical monitoring, consider querying the `Win32_PerfRawData_*` classes and manually calculating the formatted values, or using direct `pywin32` COM calls to minimize overhead. Always explicitly specify only the required properties in your WMI queries to reduce data transfer.","message":"Querying `Win32_PerfFormattedData_*` classes directly through the `wmi` package can introduce significant performance overhead. This is particularly noticeable when collecting performance metrics frequently.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new projects, evaluate more actively maintained libraries such as `PyMI` or components from `windows-tools`. For existing projects using `wmi`, be aware of the lack of ongoing support and consider migration if future compatibility or performance becomes a concern.","message":"The `wmi` library's last release was in April 2020, indicating it is no longer under active development. While it remains functional, new features or compatibility fixes for future Python or Windows versions are unlikely. More modern alternatives like `PyMI` (which offers faster execution and no `pywin32` dependency) or specialized `windows-tools` modules are available.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}