2

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?

8
  • Why are you using Select-Object at all? Commented Aug 27, 2018 at 23:40
  • It appears to work in PS : 5.0.10586.117 Commented Aug 27, 2018 at 23:50
  • You can start the Windows PowerShell 2.0 Engine: PowerShell.exe -Version 2. Works for me. Commented Aug 28, 2018 at 15:52
  • Switching to version 2 definitely works, though the main reason I upgraded to v5.1 in the first place was to use new cmdlets. I'll make sure to keep it in mind. Commented Aug 28, 2018 at 22:29
  • @TheIncorrigible1 This is just an example. My real use case is pulling multiple mailbox/email/user objects from Exchange/Skype/EWS/Azure AD and viewing various properties for them. I know Format-List will show all the information I need. I just like the table format from Select-Object/Format-Table Commented Aug 28, 2018 at 22:45

2 Answers 2

0

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.

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

1 Comment

I appreciate the work. I was hoping to avoid this as I just want to quickly & easily grab multiple properties without having to think about column width, but it looks like this is the best way to go. I don't know how much this is going to bug me yet, but if it's really bad, I'll just make a custom function that changes the width based on how many properties I select.
0

Seems to work fine for me on V5.1

enter image description here

1 Comment

You are right in your case it works because of the width of your console is enougth. Just try a native PowerShell Console and put the width configuration to 80 and you can reproduce.

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.