Phoenix2529
Phoenix2529

Reputation: 147

Convert array output to table using PowerShell

I'm exporting certificates from Windows CA which is an array which has data in below format. I want to convert into table. Any idea?

From:

$a=@(
'a=all
 b=call
',
'a=all
 b=ll'
)

Current output:

a=all
b=call

a=all
b=ll

Desired output:

a     b
all   call
all   ll

Upvotes: 0

Views: 5510

Answers (2)

Pwd9000
Pwd9000

Reputation: 142

You can also Create an empty Array and then just add values using [PsCustomObject]

$Table = @()
$Table += [Pscustomobject]@{a = "all"; b = "call"}
$Table += [Pscustomobject]@{a = "all"; b = "ll"}

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

What you have is an array of multiline strings. For the desired output you need an array of objects:

$a = @(
    [PSCustomObject]@{
        'a' = 'all'
        'b' = 'call'
    },
    [PSCustomObject]@{
        'a' = 'all'
        'b' = 'll'
    }
)

If your input data is actually a list of multiline strings with lines of the form key=value you can transform those into custom objects like this:

$a = @(
'a=all
b=call',
'a=all
b=ll'
)

$a | ForEach-Object {
    [PSCustomObject]($_ | ConvertFrom-StringData)
}

ConvertFrom-StringData converts a string with one or more lines of key=value pairs into a hashtable, which can then be cast into a custom object.

Upvotes: 2

Related Questions