2

I have a simple script that is creating three arrays with some sample objects in PowerShell. When you output these multiple objects in PowerShell it is not giving proper output. But when you check each output object variable separately it is showing correct values. Can some one please suggest how to fix this issue. I also tried with Write-Output with no luck.

Here is the script:

$d1 = @() 
$d2 = @() 
$d3 = @() 

$d1 += (New-Object -TypeName PSObject -Property ([ordered]@{"Table1"='Data1'; "Value"='Value1'}))
$d1 += (New-Object -TypeName PSObject -Property ([ordered]@{"Table1"='Data2'; "Value"='Value2'}))

$d2 += (New-Object -TypeName PSObject -Property ([ordered]@{"Table2"='Data1'; "Value"='Value1'}))
$d2 += (New-Object -TypeName PSObject -Property ([ordered]@{"Table2"='Data2'; "Value"='Value2'}))

$d3 += (New-Object -TypeName PSObject -Property ([ordered]@{"Table3"='Data1'; "Value"='Value1'}))
$d3 += (New-Object -TypeName PSObject -Property ([ordered]@{"Table3"='Data2'; "Value"='Value2'}))

$d1
$d2
$d3

And here is the out put you get after running script:

Table1 Value 
------ ----- 
Data1  Value1
Data2  Value2
       Value1
       Value2
       Value1
       Value2

Where as if we check each variable like $d1 or $d2 etc. individually, it is showing the correct values:

PS C:\Temp> $d1
Table1 Value 
------ ----- 
Data1  Value1
Data2  Value2

PS C:\Temp> $d2
Table2 Value 
------ ----- 
Data1  Value1
Data2  Value2

PS C:\Temp> $d3
Table3 Value 
------ ----- 
Data1  Value1
Data2  Value2
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
SavindraSingh
  • 878
  • 13
  • 39
  • _[...]is not giving proper output._ is not very precise. – Clijsters Dec 28 '17 at 08:19
  • 1
    @Clijsters How come? There's the actual outcome and desired output too. – vonPryz Dec 28 '17 at 08:21
  • Not really. You are saying what comes out when you type every variable seperately. Is this exactly how you want it to look?? – Clijsters Dec 28 '17 at 08:53
  • If so, why not `$d1, $d2, $d3 | % {Write-Host $_}`? – Clijsters Dec 28 '17 at 08:55
  • 1
    This is an example of PowerShell trying to be too helpful. Your tables have different columns, and PowerShell default output formatting (re-)uses the column set of the first table for displaying subsequent tables, basically displaying subsequent data sets as a single table without having to merge the data first. However, this has the side-effect that only same columns are displayed while different columns are hidden. – Ansgar Wiechers Dec 28 '17 at 10:12
  • Using `$d1 | Out-Default; $d2 | Out-Default; $d3 | Out-Default` worked for me. as suggested in the suggested answer link. – SavindraSingh Dec 28 '17 at 10:23
  • @Ansgar Wiechers: I tried to search with various different keywords for similar question but was not able to find the duplicate question that you have suggested. Thank you for pointing me to the correct duplicate question. Also, @Clijsters: thanks for the inputs but what you have suggested, didn't work, it is giving blank output with `$d1, $d2, $d3 | % {Write-Host $_}` – SavindraSingh Dec 28 '17 at 10:27

0 Answers0