Enumerate System Objects using WMI






4.82/5 (23 votes)
This article demonstrates how to query system using WMI interface

Introduction
WMI
is a technology introduced by Microsoft to support the management of operating systems and system objects in an enterprise. WMI
allows developers to use a simple, consistent mechanism to query for information or configure settings on computers or enumerate objects on computers across an enterprise. In my project, I had to enumerate the different objects like network adapters, printers, and CPUs on a machine. In the absence of WMI
, I might have to learn a lot of APIs and perform a lot of research, atleast one for each object type to be enumerated. With WMI
, my life became simple as every object on a machine can be enumerated as a class using WMI
.
Explaining more on WMI
is beyond the scope of this article. There is no proper documentation available on WMI
, so developers are not very much aware of this. This article is designed to introduce enumerating system objects and their characteristics using WMI
to developers, which I had difficulty finding. A tool to help browse the information that's available through WMI
is available with a test application called WBEMTest.exe that gets shipped with every version of an operating system. This is a very powerful app, since it provides access to almost every DCOM interface exposed by WMI
, but it is very difficult for a novice to understand.
WMI
works on almost all Microsoft ® platforms. However if WMI
is not available in any operating system, one can download "Windows Management Instrumentation (WMI) CORE 1.5 (Windows 95/98/NT 4.0)".
In the .NET Framework, WMI
is exposed in the System.Management
namespace. One can use this namespace to perform various tasks using WMI
.
Getting Started
WMIDemo
can be used for getting simple computer information. This contains WMIHelper
which exposes several methods which can be used to get specific information of the system area. Basic entity is also included in the library. These can be used for getting and displaying the computer information in any C# project.
BIOS
- Stores information related to BIOSComputerSystem
- Stores information related to computer systemDisk
- Stores information related to physical diskHotFixes
- Stores information related to the HotFixes installed in the systemLogicalDisk
- Stores information related to logical disk drivesNetworkAdapter
- Stores information related to network adaptersOS
- Stores information related to operating systemPrinters
- Stores information of printers installedProcessors
- Stores information of processorsSoundCard
- Stores information related to soundcards installed in the systemVideoController
- Stores information related to videocards installed in the system
WMIHelper
class exposes static
methods which return the filled structures for direct use. The class is made static
so there is no need for instantiation. Exposed methods are listed below:
FillHotFixes
- Finds information related to hot fixes and returns object ofHotFixes
class containing the informationFillComputerInformation
- Finds information related to computer information and returns object ofCompueterSystem
class containing the detailsFillProcessorInformation
- Finds information related to processors and returns object ofProcessors
class containing the detailsFillBiosInformation
- Finds information related to BIOS and returns object ofBIOS
class containing the detailsFillOSInformation
- Finds information related to operating system and returns object ofOS
class containing the detailsFillVideoController
- Finds information related to Video Controller and returns object ofVideoController
class containing the detailsFillSoundCard
- - Finds information related to sound cards and returns object ofSoundCard
class containing the detailsFillDisks
- Finds information related to physical disk drive and returns object ofDisk
class containing the detailsFillLogicalDisks
- Finds information related to logical disk drives and returns object ofLogicalDisk
class containing the details.FillPrinters
- Finds information related to printers and returns object ofPrinters
class containing the detailsFillNetworkAdapter
- Finds information related to network adapters and returns object ofNetworkAdapter
class containing the details
Using the Code
Use of this module is very simple. You have to call the required method directly from the class name. No instantiation is required.
WMIHelper.FillComputerInformation(machineName, userName, password, queryAres)
machineName
- Name of machine which is to be querieduserName
- Username of any authenticated user for the machinemachineName
password
- Password for the provideduserName
queryAres
- Area to be queried - this can be found on theDescriptionAttribute
of the member in theenum QueryArea
local
or machineName
can be used directly. For local machine, do not specify username
and password
. For domain user, username
will be DOMAIN\USERNAME
.
Brief Inside Code
This module uses ManagementClass
for retrieving information which is part of managed .NET and can be found under the System.Management
namespace. You will need to add a reference to System.Management.dll.

You will need to create a connection by creating an instance of ConnectionOptions
and declare the scope using ManagementScope
. Specify the query path using the ManagementPath
instance.
path = @"\\" + machineName + @"\root\cimv2";
if (userName != string.Empty && password != string.Empty)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;
mScope = new ManagementScope();
mScope.Options = options;
mScope.Path = new ManagementPath(path);
}
You are done. Now instantiate ManagementClass
, assign scope and run the query managementClass.GetInstances()
.
ManagementClass managementClass = new ManagementClass(path + ":" + queryArea);
if (mScope != null)
{
managementClass.Scope = mScope;
}
ManagementObjectCollection instances = managementClass.GetInstances();
if (instances == null)
{
return null;
}
foreach (ManagementBaseObject instance in instances)
{
//Fill information
}
References
History
23rd April, 2007: This is the first release of the code.
Modifications and feature enhancements will be done on request. I will request users to email me directly for any suggestions.