1.ps1 has 2 objects created and printed. But only one show up in the output.
Both objects show up in the following cases:
(1) write-output is done with Format-List
(2) object1 has more than 4 properties (and so it gets vertically formatted automatically)
Trying to understand the reasoning behind this behaviour.
PS C:\> cat .\1.ps1
$object1 = New-Object PSObject
$object1 | add-member NoteProperty -name pn1 -value pv1
$object1 | add-member NoteProperty -name pn2 -value pv2
$object1 | add-member NoteProperty -name pn3 -value pv3
write-output $object1
$object2 = New-Object PSObject
$object2 | add-member NoteProperty -name npn1 -value npv1
$object2 | add-member NoteProperty -name npn2 -value npv2
$object2 | add-member NoteProperty -name npn3 -value npv3
$object2 | add-member NoteProperty -name npn4 -value npv4
$object2 | add-member NoteProperty -name npn5 -value npv5
$object2 | add-member NoteProperty -name npn6 -value npv6
write-output $object2
PS C:\>
PS C:\> .\1.ps1
pn1 pn2 pn3
--- --- ---
pv1 pv2 pv3
PS C:\>
If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.
To write to the outside world's stderr by default, use [Console]::Error. WriteLine() , as recommended in the answer, at the expense of being able to capture such output inside a PowerShell session.
Summary. The $FormatEnumerationLimit variable is a neat feature of PowerShell that allows you to see more occurrences when using Format-Table . But remember: if you are using this variable in a function or a script, you should be aware of the scoping issue.
I think it has to do with the way Format-Table
works. I think by default PowerShell will either pick Format-Table
or Format-List
to display objects on the console, I'm not sure how the decision is made but obviously Format-Table
was chosen.
Format-Table
works by taking the first object and picking the properties from that. For any object that follows the first one it will only populate the columns for the properties from the first object.
You might want to try
.\1.ps1 | % { $_ | ft }
And see if that get's you what you are looking for.
If you are just trying to display the information, you should be using Write-Host
rather than Write-Output
. You want to use Write-Output
when you are sending data on in the pipeline, and shouldn't be used to simply display data.
Here is a post that gives a more in-depth answer: Which should I use: "Write-Host", "Write-Output", or "[console]::WriteLine"?
From the link:
Write-Output should be used when you want to send data on in the pipe line, but not necessarily want to display it on screen. The pipeline will eventually write it to out-default if nothing else uses it first. Write-Host should be used when you want to do the opposite. [console]::WriteLine is essentially what Write-Host is doing behind the scenes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With