Home > front end >  How to use PS calculated properties to collate data into a cell?
How to use PS calculated properties to collate data into a cell?

Time:12-15

I'm stumped on this one. I have a PS custom object with strings only and I want to build a report where I'm outputting strings of data into a new pipeline output object.

$myObjectTable | 
Select-Object @{
    n = "OldData";
    e = { 
        $_ | Select-Object name, *_old | Format-List | Out-String
    }
},
@{
    n = "NewData";
    e = {
        $_ | Select-Object name, *_new | Format-List | Out-String
    }
}

Running this produces blank output.

I tried running the code above with just the $_ object in the expressions, but I only got ... as the output. Wrapping the expressions in parenthesis did not change the output.

CodePudding user response:

The ... as property value means that the value is either a multi-line string or it just didn't fit in a tabular format. See Using Format commands to change output view more details.

You can fix those empty lines added by Out-String using Trim. Then if you want to properly display this object having multi-line property values, Format-Table -Wrap will be needed.

Here is a little example:

[pscustomobject]@{
    Name = 'foo'
    Something_Old = 123
    Something_New = 456
} | ForEach-Object {
    [pscustomobject]@{
        OldData = $_ | Format-List name, *_old | Out-String | ForEach-Object Trim
        NewData = $_ | Format-List name, *_new | Out-String | ForEach-Object Trim
    }
} | Format-Table -Wrap

Resulting object would become:

OldData                                  NewData
-------                                  -------
Name          : foo                      Name          : foo
Something_Old : 123                      Something_New : 456
  • Related