12

Call me stupid, but I'm losing my hair with this one.

I have two results from a Get-WmiObject:

$cpu = Get-WmiObject -Class Win32_Processor 
$mb = Get-WmiObject -Class Win32_BaseBoard

Now, I can filter and output a CSV file from each one:

$cpu | Select-Object Name, Description | ConvertTo-Csv -NoTypeInformation

and

$mb | Select-Object Manufacturer, Product | ConvertTo-Csv -NoTypeInformation

But... How the hell could I join these two outputs and make a single CSV output from both? Something like:

( 
  ($cpu | Select-Object Name, Description) + 
  ($mb | Select-Object Manufacturer, Product) 
) | ConvertTo-Csv -NoTypeInformation

(of course, this syntax is invalid. Just to show the point)

rookie
  • 205
  • 1
  • 3
  • 6

7 Answers7

12

You need Powershell V2 for the following.

$cpu = Get-WmiObject -Class Win32_Processor 
$mb = Get-WmiObject -Class Win32_BaseBoard
$props = @{            
    Name          = $cpu.Name
    Description   = $cpu.Description
    Manufacturer  = $mb.Manufacturer
    Product       = $mb.Product
    }
New-Object PSObject -Property $props | ConvertTo-Csv -NoTypeInformation
dan-gph
  • 16,301
  • 12
  • 61
  • 79
  • While this works perfectly for OPs question, those coming here looking for joining multiple rows of data will have to pursue an alternative strategy. – KyleMit Jan 15 '16 at 16:57
  • 1
    @KyleMit, `Select-Object` with calculated properties might be useful to you. – dan-gph Jan 15 '16 at 22:37
5

There's a Join-Object function on PoshCode for doing this, but it's buried inside Join-Collection and not exported.

 function Join-Object {
    Param(
       [Parameter(Position=0)]
       $First
    ,
       [Parameter(Position=1,ValueFromPipeline=$true)]
       $Second
    )
    BEGIN {
       [string[]] $p1 = $First | gm -type Properties | select -expand Name
    }
    Process {
       $Output = $First | Select $p1
       foreach($p in $Second | gm -type Properties | Where { $p1 -notcontains $_.Name } | select -expand Name) {
          Add-Member -in $Output -type NoteProperty -name $p -value $Second."$p"
       }
       $Output
    }
 }

Once you've defined that, you can use it like this:

Join-Object (Get-WmiObject -Class Win32_Processor) (Get-WmiObject -Class Win32_BaseBoard) | 
Select Name, Description, Manufacturer, Product

Or keep your variables, and do it like:

$cpu = Get-WmiObject -Class Win32_Processor 
$mb = Get-WmiObject -Class Win32_BaseBoard

Join-Object $cpu $mb  | Select Name, Description, Manufacturer, Product
Jaykul
  • 15,370
  • 8
  • 61
  • 70
1

How about like this?

echo $cpu $mb | Select-Object Name, Description, Manufacturer, Product | ConvertTo-Csv -NoTypeInformation
YOU
  • 120,166
  • 34
  • 186
  • 219
  • Almost! This actually returns four columns, but in *TWO* lines. Each line containing two values and two nulls. Now, if we could make a kind of "union"... – rookie Dec 05 '09 at 04:56
  • There is `-join "a","b"`, I am trying to figure out how to use it. – YOU Dec 05 '09 at 05:22
0

You can always add whatever properties you need to a custom powershell object that has what you need and then dump the cvs. Take a look at this page and see if it suites what you need.

rerun
  • 25,014
  • 6
  • 48
  • 78
0

You can start with either object and then add properties from the other:

$cpu | Select-Object Name, Description | 
  add-member noteproperty Manufacturer $mb.Manufacturer -PassThru | 
  add-member noteproperty Product      $mb.Product      -PassThru
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
0

Is this what you are looking for? Somewhat longer then I expected, because both $cpu and $mb have Name and Manufacturer properties. But the prime idea is the custom objects through hashing.

($cpu | Select-Object Name, Description),($mb | Select-Object Manufacturer, Product) | 
       Select-Object @{name='NameOrManu';expr={$_.Name + $_.Manufacturer}},  
                     @{name='DescrOrProd';expr={$_.Description + $_.Product}} | 
       ConvertTo-Csv -NoTypeInformation
0

The Join-Object, described in In Powershell, what's the best way to join two tables into one? has a lot more capabilities, but can also be used like this:

$cpu | Select-Object Name, Description | Join-Object ($mb | Select-Object Manufacturer, Product)
iRon
  • 20,463
  • 10
  • 53
  • 79