Merge two PSCustomObjects into one- PowerShell

Viewed 1041

I need help in PowerShell to combine two outputs or two PSCustomObjects into One. For example,

$services = Get-Service | Select Name, Starttype,Status
$processes = Get-Process | Select ID

I need the output with the table headers Name, Starttype, Status, ID

I have already tried creating CSV and joining them but the problem is Process ID starts when the entire output ends for the services. I need them to a parallel.

Second I have tried to create PSCustomObjects but no luck.

Please help me with the PowerShell code.

Actual code that I'm trying to achieve.

**$exclusionItems = @()
$OasHighItems = @()   
foreach($item in $items){
    $exclusionItems  += [PSCustomObject]@{
        EXCLUSION_BY_NAME_OR_LOCATION = $item.EXCLUSION_BY_NAME_OR_LOCATION
        EXCLUSION_EXCLUDE_SUBFOLDERS = $item.EXCLUSION_EXCLUDE_SUBFOLDERS
        EXCLUSION_ON_READ= $item.EXCLUSION_ON_READ
    }
    
}
foreach($oas in $oashigh){
    $oashighItems += [PSCustomObject]@{
        OAS_PROCESSES_LIST = $oas
    }
}
$Array = @()
$Array = $exclusionItems,$oashighItems
$Array | Update-FirstObjectProperties | Export-Excel $ExcelParams -TableName Table -Show**
1 Answers

I'm assuming you want to join the two objects by their names, i.e. match the Process-Name with the Service-Name. For this you can loop over all processes & services, keep only those where service-name equals process-name, and use a calculated property to merge the result into one object:

$services = Get-Service;
Get-Process | ForEach-Object {$p = $_;  $services |
                Where-Object{$p.ProcessName -eq $_.Name} |
                    Select-Object Name,StartType,Status,@{n='ID';e={$p.ID}}} 

The output on my machine is:

Name                  StartType  Status    ID
----                  ---------  ------    --
CcmExec               Automatic Running 14856
CmRcService           Automatic Running  5748
FusionInventory-Agent Automatic Running  5996
IBMPMSVC              Automatic Running  3540
IntelAudioService     Automatic Running  6104
... and so on ...
Related