2

I'm creating a custom object with two properties. The first is a string, basically a key, and the second is the output of a function which returns an array. I'm then piping the results into Format-Table and grouping by the string property. What I'd like to see is each element of the array property on a separate row in the output. Instead, Format-Table is displaying the array on a single row.

Is there any way of formatting the output so each element of the array property is displayed on a separate row?

Here's some code that illustrates the problem:

function Get-Result([string]$lookup)
{
    if ($lookup -eq "first")
    {
        return @("one", "two")
    }
    else
    {
        return @("three")
    }
}

$a = "first", "second"

$a | 
    Select-Object @{Name="LookupValue"; Expression={$_}}, `
        @{Name="Result"; Expression={Get-Result $_}} | 
    Format-Table -GroupBy LookupValue

And this is what it outputs:

   LookupValue: first

LookupValue Result    
----------- ------    
first       {one, two}


   LookupValue: second

LookupValue Result
----------- ------
second      three 

What I'd like to see is:

   LookupValue: first

LookupValue Result    
----------- ------    
first       one  
first       two    


   LookupValue: second

LookupValue Result
----------- ------
second      three 

2 Answers 2

2

Format-cmdlets are not going to create objects that do not exist. Your example has a result value that contains an array. If you do not want that to be an array but instead part of two distinct objects then you need to split that out somehow or change your creation process to make those multiple/missing objects.

The former by adding another inner loop that processes multiple "results"

$a | ForEach-Object{
    $element = $_
    Get-Result $element | ForEach-Object{
        $_ | Select-Object @{Name="LookupValue"; Expression={$element}},
            @{Name="Result"; Expression={$_}}
    }
} | Format-Table -GroupBy LookupValue

So for every output of Get-Result a new object is created reference the current LookupValue in the pipeline which is represented by $element.

That is a little clunky so I also want to add an example of the ladder where we change up your creation process

function Get-Result{
    param(
        [Parameter(
            ValueFromPipeline)]
        [string]$lookup
    )

    process{
        switch($lookup){
            "first"{
                [pscustomobject]@{LookupValue=$lookup;Result="one"},
                [pscustomobject]@{LookupValue=$lookup;Result="two"}
            }
            default{
                [pscustomobject]@{LookupValue=$lookup;Result="three"}
            }

        }
    }
}

$a = "first", "second"
$a | Get-Result | Format-Table -GroupBy LookupValue

Create a function that accepts pipeline input and spits out pscustomobjects based on the $lookup. I used a switch here in case your real application of this had more conditions than what you show in your sample.

Note

In order for -GroupBy to work the data set is supposed to be sorted. So if you have issues with real data not appearing correctly .... sort it first.

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

Comments

2

One way of getting it -

function Get-Result([string]$lookup) {
    if ($lookup -eq "first") {
        Write-Output @( 
            @{ LookupValue=$lookup; Result="one" },
            @{ LookupValue=$lookup; Result="two" }
        )
    }
    else {
        Write-Output @(
            @{ LookupValue=$lookup; Result="three" }
        )
    }
}

"first", "second" | % {  Get-Result($_) } | % { [PSCustomObject]$_ } | Format-Table LookupValue, Result -GroupBy LookupValue

Output:

    LookupValue: first

LookupValue Result
----------- ------
first       one   
first       two   


   LookupValue: second

LookupValue Result
----------- ------
second      three 

Comments

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.