-1

I wish to display an array of hastables as a Table. I found several threads dealing with this issue, but so far no solution has worked for me (see here and here).

My array consists of 18 hashtables in this form:

Array = @(
    @{Company=1} 
    @{Company=2} 
    @{Company=3} 
    @{Contact=X} 
    @{Contact=Y} 
    @{Contact=Y} 
    @{Country=A} 
    @{Country=B} 
    @{Country=C} 
)

I would like to get the following output:

Company Contact Country
------------------------------
1                   X                 A
2                   Y                 B
3                   Z                 C

I've tried the following:

$Array | ForEach-Object { New-Object -Type PSObject -Property $_ } | Format-Table

That displays the following:

Company
----------
1
2
3

A Format-List works better; I then get this:

Company: 1 2 3
Contact: X Y Z
Country: A B C

Is there any way to accomplish my desired output?

1
  • Please provide a working data set as example... Commented Sep 29, 2022 at 9:56

1 Answer 1

2

You can do the following if you want to work with CSV data and custom objects:

$Array = @(
    @{Company=1} 
    @{Company=2} 
    @{Company=3} 
    @{Contact='X'} 
    @{Contact='Y'} 
    @{Contact='Z'} 
    @{Country='A'} 
    @{Country='B'} 
    @{Country='C'} 
)

$keys = $Array.Keys | Get-Unique
$data = for ($i = 0; $i -lt $Array.($Keys[0]).Count; $i++) {
    ($Keys | Foreach-Object { $Array.$_[$i] }) -join ','
}
($Keys -join ','),$data | ConvertFrom-Csv | Format-Table

Explanation:

Since $Array is an array of hash tables, then the .Keys property will return all the keys of those hash tables. Since we only care about unique keys when building our object, Get-Unique is used to remove duplicates.

$Array.($Keys[0]).Count counts the number of items in a group. $Keys[0] here will be Company. So it returns the number of hash tables (3) that contain the key Company.

$Array.Company for example returns all values from the hash tables that contain the key Company. The Foreach-Object loops through each unique key's value at a particular index ($i). Once each key-value is read at a particular index, the values are joined by a comma.

When the loop completes, ($Keys -join ','),$data outputs the data in CSV format, which is piped into ConvertFrom-Csv to create a custom object.

Note: that if your data contains commas, you may want to consider the alternative method below.


Alernatively, you can work with hash tables and custom objects with the following:

$Array = @(
    @{Company=1} 
    @{Company=2} 
    @{Company=3} 
    @{Contact='X'} 
    @{Contact='Y'} 
    @{Contact='Z'} 
    @{Country='A'} 
    @{Country='B'} 
    @{Country='C'} 
)
$keys = $Array.Keys | Get-Unique
$data = for ($i = 0; $i -lt $Array.($Keys[0]).Count; $i++) {
    $hash = [ordered]@{}
    $Keys | Foreach-Object { $hash.Add($_,$Array.$_[$i]) }
    [pscustomobject]$hash
}
$data | Format-Table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.