Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell write-output missing information - prints only 1st object [duplicate]

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:\>
like image 297
v2b Avatar asked Aug 02 '13 16:08

v2b


People also ask

How do I use AutoSize in PowerShell?

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.

How do you write to stderr in PowerShell?

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.

What is FormatEnumerationLimit?

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.


2 Answers

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.

like image 147
Stanley De Boer Avatar answered Nov 15 '22 07:11

Stanley De Boer


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.

like image 22
BlackHatSamurai Avatar answered Nov 15 '22 07:11

BlackHatSamurai