Powershell "join" Powershell "join" powershell powershell

Powershell "join"


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


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_BaseBoardJoin-Object $cpu $mb  | Select Name, Description, Manufacturer, Product


How about like this?

echo $cpu $mb | Select-Object Name, Description, Manufacturer, Product | ConvertTo-Csv -NoTypeInformation