powershellpowershell-5.0console-output

PowerShell v5.1 Select-Object Console Output Is Different from v3.0


I just upgraded PowerShell from v3.0 to v5.1 and noticed that Select-Object and Format-Table's console outputs behave very differently. If a property's value is too long, all later properties are shunted out of the console output entirely (I can see all values are still passed on -- just suppressed in the console output). I'd like an easy way to replicate the old behavior of 2.0/3.0 (4.0?) where values are truncated to fit all the properties in the console, as it's much easier to compare data at a glance, but I can't figure out a way to do this.

Here's an example: I make an array of hash tables then try to view the output in a 120 character width console:

$array = @()
$array += New-Object PSObject -Property @{Name="Test1";Value1="samplestring";Value2="Omitted Text"}
$array += New-Object PSObject -Property @{Name="Test2";Value1="Much longer string. More than 120 characters, so that we can suppress Value2's console output. This sentence ought to do it.";Value2="Omitted Text"}
$array | select Name,Value1,Value2

In PS 2.0 and 3.0, the output is just what I want:

Name                                    Value1                                  Value2
----                                    ------                                  ------
Test1                                   samplestring                            Omitted Text
Test2                                   Much longer string. More than 120 ch... Omitted Text

...but in 5.1, it seems to automatically apply Format-Table -AutoSize and gives me this:

Name  Value1
----  ------
Test1 samplestring
Test2 Much longer string. More than 120 characters, so that we can suppress Value2's console output. This sentence o...

I've tried fiddling around with Format-Table's calculated properties, but I can't get the width property to work, and, honestly, specifying the width of each property is too much work for commands I'm typing & running on the fly. Is there some other command I'm missing, or am I stuck regretting my upgrade?


Solution

  • I wasn't able to find a solution to change the behavior back to the old versions, However: I was able to write something that may suite your needs.

    $array = @()
    $array += New-Object PSObject -Property 
    @{Name="Test1";Value1="samplestring";Value2="Omitted Text"}
    $array += New-Object PSObject -Property @{Name="Test2";Value1="Much longer string. More than 120 characters, so that we can suppress Value2's console output. This sentence ought to do it.";Value2="Omitted Text"}
    
    $array | Format-table -Property @{ e='name'; width=40 }, `
                                    @{ e='value1'; width=40 }, `
                                    @{ e='value2'; width=40 }
    

    Also, I figured that I'd put my two sense out there on an unrelated matter. As arrays scale in size, the += operator is bad for performance, because it has to (to the best of my understanding) recopy the entire array and then add the next element to it. If you're working with a large dataset and doing lots of adding and removing entries, I'd recommend using a list.