Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Is there a way to query multiple WMI objects and select required values from each object and export to Excel?

I want to achieve: Get the computer name and owner's name from the win32_computersystem class and serial number from the win32_bios class and export the results in a CSV to get the complete inventory of the computer systems in my organization.

How can it be acheived?

share|improve this question
    
Yes, this can be done and it's a fairly common scenario. How much research have you done into this, and what have you tried to achieve the goal? And why aren't you using an already-built tool like SCCM that's designed to inventory the systems on your network and can produce this report automatically for you? – alroc Nov 21 '13 at 14:15

Create a custom object with desired properties. Use New-Object to create one. Use Add-Member to add noteproperty members to the object. Get data with WMI (Get-WmiObject - alias gwmi) and populate object's properties with those.

Collect objects into an array and export it as CSV with Export-Csv.

Try it like so,

$computers = @("System1", "System2") # An array of computer names.
$arr = @() # Array for CSV export

foreach($comp in $computers){
  $o = new-object  psobject # New object

  # Add Owner property and set its value with a WMI call
  $o | add-member -membertype noteproperty -name Owner -value (gwmi win32_computersystem -computername $comp).PrimaryOwnerName

  # Ditto for Serial property
  $o  | add-member -membertype noteproperty -name Serial -value (gwmi win32_bios -computername $comp).SerialNumber

  $arr += $o
}

# Export the object data as CSV
$arr | select -property Owner,Serial | export-csv -notypeinformation c:\temp\ownerdata.csv
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.