0

I am having two variables which are both system.array type. I want to merge them and get the output in Out-grid view.

The first Variable is $out

echo $out

number            : CTASK19496
state             : Assigned
User Name         : Ana
CI Name           :
comments          :
work_notes        :
short_description : Implementation Task
opened_at         : 24/11/2020 01:37:40


number            : CTASK000501
state             : Assigned
User Name         : Ranjay 
CI Name           :
comments          :
work_notes        :
short_description : Implementation Task
opened_at         : 24/11/2020 03:50:16


number            : CTASK000169
state             : Assigned
User Name         : Dan
CI Name           :
comments          :
work_notes        :
short_description : configure the full 
opened_at         : 25/11/2020 14:47:1

The second Variable is $data

echo $data

end_date
--------
10/11/2020 23:59:59
25/11/2020 23:59:59
26/11/2020 23:59:59

code I tried is

$Shortened = $out + $out1 | ForEach-Object {
$_ | Select-Object number, end_date
 }

$Shortened | Format-Table -AutoSize

Output I am getting is

 number        end_date
------        --------
CTASK19496
CTASK000501
CTASK000169
              03/12/2020 23:59:59
              04/12/2020 23:59:00
              30/11/2020 21:30:00

Output what I want is as below

 number        end_date
------        --------
CTASK19496    03/12/2020 23:59:59
CTASK000501   04/12/2020 23:59:00
CTASK000169   30/11/2020 21:30:00
zett42
  • 25,437
  • 3
  • 35
  • 72
GURU SINGH
  • 149
  • 7
  • if the 2 arrays are in sync, you can iterate thru one and buld a `[PSCustomObject]` with the iterated collection and the same index number in the other collection. – Lee_Dailey Nov 25 '20 at 15:38
  • Does this answer your question? [How can I "zip" two arrays in PowerShell?](https://stackoverflow.com/questions/43122000/how-can-i-zip-two-arrays-in-powershell) – zett42 Nov 25 '20 at 16:11
  • To apply [this excellent answer](https://stackoverflow.com/a/44055098/7571258) to your data: `Select-Zip $out $data -ResultSelector { param($a, $b) [PSCustomObject]@{ number = $a.number; end_date = $b.end_date }}` – zett42 Nov 25 '20 at 16:14
  • Does this answer your question? [Combine two CSV's - Add CSV as another Column](https://stackoverflow.com/questions/36574792/combine-two-csvs-add-csv-as-another-column) – iRon Nov 25 '20 at 16:19

1 Answers1

1

Ass Lee_Daily commented, it's assumed each row in each array are related. You can use a for loop like this

for($i = 0; $i -lt $out.count; $i++)
{
    [PSCustomObject]@{
        number   = $out[$i].number
        end_date = $out1[$i].end_date
    }
}

output

number      end_date           
------      --------           
CTASK19496  03/12/2020 23:59:59
CTASK000501 04/12/2020 23:59:00
CTASK000169 30/11/2020 21:30:00

Just capture to a variable and then send to Out-GridView

$output = for($i = 0; $i -lt $out.count; $i++)
{
    [PSCustomObject]@{
        number   = $out[$i].number
        end_date = $out1[$i].end_date
    }
}

$output | Out-GridView
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13