How to export all rows of a WMI query to a file?

AAsk picture AAsk · Mar 8, 2011 · Viewed 27k times · Source

Given a query such as

SELECT * FROM WIN32_PROCESS
  1. Is there a way to interrogate the result object for the names of the columns returned?
  2. Write all the rows in the result object to a text file, say

Answer

Helen picture Helen · Mar 9, 2011

Is there a way to interrogate the result object for the names of the columns returned?

Yes. Each WMI object has the Properties_ collection that provides information about that object's properties. To get the names of properties available in an object, enumerate the Properties_ collection and check each item's Name.

Write all the rows in the result object to a text file, say

Enumerate all the rows and use the FileSystemObject to write them to the desired text file. Pseudocode:

create a text file and open it for writing

for each object in the result set
  for each property in the object
    write the property value to the file

close the file


Alternatively, you could use wmic to do all the work for you:

wmic /output:e:\processes.txt process get /all
wmic /output:e:\processes.csv process get /all /format:csv