I am frustrated beyond belief with Powershell at the moment, because I feel stupid for spending 2 whole work days figuring out a (most likely super simple) solution for the following problem: I would like to convert two arrays ($HeaderCells, $DataCells) into a table and nothing seems to work. I tried PSObjects, Arrays, Hash Tables, data tables... Here is my code:
$Table = @()
$HeaderCells = @("Company","Country")
$DataCells = @("Test Inc.","Misc Corp.","USA","UK")
foreach ($HeaderCell in $HeaderCells)
{
foreach ($DataCell in $DataCells)
{
$Table += New-Object -TypeName PSObject -Property @{$HeaderCell=$DataCell}
}
}
$Table
my Output is:
Company
-------
Test Inc.
USA
Misc Corp.
UK
I would like to get two columns (Company and Country), but no matter what I try (even the most nested for loops, I always end up overwriting variables or just getting errors.
My actual use case is actually a bit more complicated (extracting a table from a html page), but solving this part will allow me to continue, I hope.
My desired output would be to have the HeaderCells as the Headers and the DataCells as the Rows, so something like this:
Company Country
------- --------
Test Inc. USA
Misc Corp. UK