Search code examples
powershellazure-active-directory

Powershell: Create PSCustomObject from Array


I have a PowerShell-Object $foo with 2 properties: Column and Row. Each Property contains an array of Items:

$foo.Column
{Cost, UsageDate, Currency}

$foo.Row
{120, 20230301, USD, 150, 20230302, USD, 200, 20230303, USD ... }

How do I get a PS-CustomObject $bar from it that looks like this

$bar | Format-Table

Cost     UsageDate     Currency
----     ---------     --------
120      20230301      USD
150      20230302      USD
200      20230302      USD

Solution

  • Thanks guys! I 've solved it this way:

    Creating an empty Object first:

    $temp = @()
    

    Adding the Header to the Object

    $temp = @()
    $temp += $foo.columns.name -join ";"
    

    Adding the rows to the Object:

    $temp += $foo.rows -replace(" ",";")
    

    And convert it:

    $bar = $temp | ConvertFrom-Csv -Delimiter ";"