Windows Management Instrumentation (WMI) Python Library
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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha Querying `Win32_PerfFormattedData_*` classes directly through the `wmi` package can introduce significant performance overhead. This is particularly noticeable when collecting performance metrics frequently.
- deprecated 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.
Install
-
pip install wmi
Imports
- WMI
import wmi c = wmi.WMI()
Quickstart
import wmi
import os
# Connect to the local machine WMI service
# For remote connection, set WMI_USERNAME and WMI_PASSWORD environment variables:
# c = wmi.WMI(computer="remote_machine_ip", user=os.environ.get('WMI_USERNAME', ''), password=os.environ.get('WMI_PASSWORD', ''))
try:
c = wmi.WMI()
# Get information about the operating system
for os_info in c.Win32_OperatingSystem():
print(f"OS Name: {os_info.Caption}")
print(f"Version: {os_info.Version}")
print(f"Service Pack: {os_info.CSDVersion}")
print(f"Architecture: {os_info.OSArchitecture}")
print(f"Free Physical Memory: {int(os_info.FreePhysicalMemory) / (1024**2):.2f} GB")
print("\n--- Listing Running Processes (top 5) ---")
# Get information about running processes (top 5)
for process in c.Win32_Process()[:5]:
print(f"Process: {process.Name}, ID: {process.ProcessId}, Status: {process.Status}")
print("\n--- Listing Stopped Services (Auto Start) ---")
# Find and list automatically starting services that are currently stopped
for s in c.Win32_Service(StartMode="Auto", State="Stopped"):
print(f"Service: {s.Caption}, State: {s.State}")
except wmi.x_wmi as e:
print(f"WMI Error: {e}")
if "Access is denied" in str(e):
print("Ensure the script is run with administrator privileges, or correct remote authentication.")
elif "RPC server is unavailable" in str(e):
print("Ensure the remote machine is reachable and the WMI service is running.")
except Exception as e:
print(f"An unexpected error occurred: {e}")