0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"}

1 Comment

Appending to an array is a slow operation and should be avoided where possible.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.